如何检查用户是否已点击屏幕的特定部分?

时间:2018-05-17 06:08:04

标签: android touch motionevent

嗨,我是android studio的新手,我有一个可以绘制一些形状的应用程序。目前,当我点击屏幕时,应用会删除最后绘制的形状。

但是,我需要它才能删除形状,只有当用户点击画布的左上角1/16后跟画布的右下角1/16时?

这就是我试过的。我的getWidth和getHeight有什么问题吗?

这是我的onViewCreated

 public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getWidth=view.getWidth();
    getHeight=view.getHeight();

OnsingleTap确认方法

 public boolean onSingleTapConfirmed(MotionEvent e) {
        Cursor cursor = getLastShape();
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
        }
        int x1 = (int) e.getX();
        int  y1 = (int) e.getY();

        if((x1<1/16*getWidth) &&(y1<1/16 * getHeight)){
            int x2 = (int) e.getX();
            int  y2 = (int) e.getY();
            if((x2>=getWidth-1/16*getWidth) && (y2>=getHeight-1/16*getHeight)) {
                resolver.delete(Shape.CONTENT_URI, "_id=" + cursor.getInt(cursor.getColumnIndex(Shape.ID)), null);
            }

        }

     return true;



}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码查找特定视图的单击和双击。

public class GestureSingleDoubleTap extends AppCompatActivity {

TextView txtTap;
GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gesture_single_double_tap);

    onInit();
}

private void onInit() {

    txtTap = findViewById(R.id.txtTap);
    gestureDetector = new GestureDetector(this, new GestureListener());

    txtTap.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

}


private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Toast.makeText(GestureSingleDoubleTap.this, "Double Tap Occurred", Toast.LENGTH_SHORT).show();
        return true;
    }

    // event when single tap occurs
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Toast.makeText(GestureSingleDoubleTap.this, "Single Tap Occurred", Toast.LENGTH_SHORT).show();
        return super.onSingleTapConfirmed(e);
    }
}

}

xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.example.GestureSingleDoubleTap">

    <TextView
      android:id="@+id/txtTap"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:padding="20dp"
      android:text="Tap me Single or Double" />

</RelativeLayout>