如何检查用户是否点击了屏幕的特定部分?
How to check whether the user has tapped on a specific part of the screen?
嗨,我是 android 工作室的新手,我有一个可以绘制一些形状的应用程序。目前,当我点击屏幕后,应用程序会删除最后绘制的形状。
但是,只有当用户单击 canvas 的左上 1/16,然后单击 canvas 的右下 1/16 时,我才需要删除形状?
这就是我 tried.Is 我的 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;
}
您可以使用以下代码在特定视图上找到单击和双击。
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>
嗨,我是 android 工作室的新手,我有一个可以绘制一些形状的应用程序。目前,当我点击屏幕后,应用程序会删除最后绘制的形状。
但是,只有当用户单击 canvas 的左上 1/16,然后单击 canvas 的右下 1/16 时,我才需要删除形状?
这就是我 tried.Is 我的 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;
}
您可以使用以下代码在特定视图上找到单击和双击。
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>