如何在屏幕上处理触摸事件和手势事件

时间:2017-09-24 07:32:57

标签: java android

我想设置触控侦听器并使用右键触控

<创建方法中的

我为主要版面设置了触控侦听器

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

我如何处理手势事件?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码示例

B

希望回答这个问题 编辑 要使您的父布局正常工作,请在父布局中添加以下代码,例如相对布局

public class BinaryToDecimal {
    public static void main(String[] args) {
        System.out.print("Input first binary number: ");
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        while(!"q".equalsIgnoreCase(line.trim())){
            try{
                System.out.println(Integer.parseInt(line,2));
                System.out.print("Input next binary number: ");          
                line = in.nextLine();
            }
            catch(Exception e){
                System.out.println("Not a binary number");
            }
        }
    }
}