2014-12-05 2 views
0

ImageView에서 위치를 가져 오기 위해 일부 코드를 작성했습니다. MotionEvent에 대한 매개 변수를 추가하는 방법

private void touchLocation(MotionEvent e){ 
float x = e.getX(); 
float y = e.getY(); 

String message = "Location is x = " + x + ", y = " + y "."; 

Context context = getApplicationContext(); 
int duration = Toast.LENGTH_LONG: 

Toast toast = Toast.makeText(context, message, duration); 
toast.show(); 

}는에서 onCreate() 메소드에서

가 어떻게이 함수를 호출해야

: 다음은 내 코드는? 어떤 제안이라도 대단히 감사하겠습니다. 어떻게 MotionEvent 클래스로 객체를 인스턴스화해야합니까?

답변

1
imageView.setOnTouchListener(new OnTouchListener(){ 

    @Override 
    public boolean onTouch(View v, MotionEvent e) { 

    float x = e.getX(); 
    float y = e.getY(); 

    String message = "Location is x = " + x + ", y = " + y "."; 

    // more code 

    return true; 
    } 
}); 
+0

"형식 OnOutTouchListener (View.OnTouchListener) 형식의 인수는 새 OnTouchListener() {}) 인수에는 적용 할 수 없습니다."오류가 발생합니다. 왜? 어떻게 해결할 수 있을까요? –

+0

리스너를 올바르게 가져 왔는지 확인하십시오. 파일 가져 오기에서 android.view.View.OnTouchListener;가 필요합니다. –

+0

감사합니다. Petr Duchek. 그것은 모든 문제를 해결합니다. –

관련 문제