问题
My activity should record the coordinates of a point on the screen, which the user can move. Specifically, the user touches the screen and appears on the item, which will be able to move. When you lift your finger, the coordinates of the point should be recorded.Please for your advice.
回答1:
EDITED Here is whole simple activity which Toasts the user touch down and touch up co-ordinates
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(getApplicationContext(), "Touch Down x="+x+" y="+y ,Toast.LENGTH_SHORT).show();
//Handle Touch Down
break;
case MotionEvent.ACTION_MOVE:
//Handle Touch Move
break;
case MotionEvent.ACTION_UP:
Toast.makeText(getApplicationContext(), "Touch Up x="+x+" y="+y ,Toast.LENGTH_SHORT).show();
//Handle Touch Up
break;
}
return false;
}
}
来源:https://stackoverflow.com/questions/17922650/android-recording-coordinates-of-the-point-in-screen