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.

有帮助吗?

解决方案

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;
}

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top