如何在安卓设备上检测触摸输入

22

目前,我只想检测屏幕是否被按下,并显示日志消息以确认它发生了。到目前为止,我的代码是基于CameraPreview示例代码修改而来的(最终将会拍照),因此大部分代码位于扩展了SurfaceView类的类中。SDK示例代码的API版本号为7。

5个回答

35

尝试使用以下代码来检测触摸事件。

mView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //show dialog here
        return false;
    }
});

使用Activity的showDialog(int)方法显示对话框。您必须实现onCreateDialog()。有关详细信息,请参阅文档。


1
这里的mView是什么?我对这行代码不熟悉,请分享你的答案。 - Md. Masum Billah
@Md.MasumBillah 这是一个“View”对象。请参阅此答案以了解如何创建它。 - ggorlen

19

这里有一个简单的示例,展示如何检测简单的触摸事件、获取坐标并显示一个提示信息。该示例中包含三个事件:按下(Action Down)、移动(Move)和抬起(Action Up)。

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private boolean isTouch = false;

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int X = (int) event.getX();
        int Y = (int) event.getY();
        int eventaction = event.getAction();

        switch (eventaction) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                isTouch = true;
                break;

            case MotionEvent.ACTION_MOVE:
                Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                break;

            case MotionEvent.ACTION_UP:
                Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

我想在屏幕顶部仅触摸一个宽度和高度为200的视图时调用此事件。如何实现?目前on-touch监听整个屏幕。 - user14016776

6
我是这样做的:
public class ActivityWhatever extends Activity implements OnTouchListener
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);

        //the whole screen becomes sensitive to touch
        mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
        mLinearLayoutMain.setOnTouchListener(this);
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO put code in here

        return false;//false indicates the event is not consumed
    }
}

在您的视图的 XML 中,指定:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main">

    <!-- other widgets go here-->

</LinearLayout>

4

我尝试了很多次,终于在两天后找到了一种解决方案,可以检测屏幕触摸。

Kotlin:

如果您有底部导航栏,并希望在触摸时隐藏...请尝试以下方法!

Activity.dispatchTouchEvent(MotionEvent) - 这允许您的Activity在事件分派到窗口之前拦截所有触摸事件。

  override fun dispatchTouchEvent(event: MotionEvent): Boolean {

   if (event.getAction() === MotionEvent.ACTION_DOWN) {
       if (event.getAction() === MotionEvent.ACTION_DOWN) {




       }
   } else if (event.getAction() === MotionEvent.ACTION_MOVE) {
       tabLayout.visibility = View.GONE
       tv_chat.visibility = View.GONE



   } else if (event.getAction() === MotionEvent.ACTION_UP) {

       tabLayout.visibility = View.VISIBLE
       tv_chat.visibility = View.VISIBLE

   }
    return super.dispatchTouchEvent(event)
}

如何在用户触摸 Snackbar 边界以外的任何地方时隐藏它:https://gist.github.com/and291/ce5704c4163f8dcd9d06b1ab6a9850cf - Andrey Busik

2

//当手指触摸视图时可见。当手指抬起时不可见。

    hintView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
          if(event.getAction()==MotionEvent.ACTION_MOVE){
                hintText.setVisibility(View.VISIBLE);
            }else if(event.getAction()==MotionEvent.ACTION_UP){
              hintText.setVisibility(View.GONE);

            }

            return true;
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接