触摸事件和手势事件,我想触摸监听器设置并在触摸右如何处理在屏幕上
使用建立方法我设置触摸监听器为主要布局
mainActivityLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
XML文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainActivityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
然后覆盖的onTouchEvent,但没有工作
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(getBaseContext(), "On Touch", Toast.LENGTH_SHORT).show();
if (event.getAction() == *TouchRight*)
Toast.makeText(getBaseContext(), "Touch Right", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
我如何处理手势事件?
这意味着你在根布局上设置触摸监听器? –
@AbdulWaheed是的,我想为屏幕的所有区域设置 – Bahadori
我已经更新了我的答案将这些xml属性添加到您的主父布局这将为你工作 –