如何检测 Android 上的触摸输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3142670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:54:37  来源:igfitidea点击:

How do I detect touch input on the Android

androideventsinputtouch

提问by RyoxSinfar

Right now all I am trying to do is detect when the screen is pressed and then display a log message to confirm it happened. My code so far is modified off of the CameraPreview sample code (it will eventually take a picture) so the bulk of the code is in a class that extends SurfaceView. API for the example code from the SDK is 7.

现在我要做的就是检测何时按下屏幕,然后显示一条日志消息以确认它发生了。到目前为止,我的代码是根据 CameraPreview 示例代码修改的(它最终会拍照),因此大部分代码位于扩展 SurfaceView 的类中。SDK 中示例代码的 API 是 7。

回答by Dariusz Bacinski

Try code below to detect touch events.

试试下面的代码来检测触摸事件。

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

To show dialog use Activity method showDialog(int). You have to implement onCreateDialog(). See documentation for details.

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

回答by MSA

Here is a simple example on how to detect a simple on touch event, get coords and show a toast. The event in this exmple are Action Down, Move and 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;
    }
}

回答by Someone Somewhere

I did it like this:

我是这样做的:

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

in the xml of your view, specify:

在视图的 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>

回答by Rohit Shrivastava

I have tried alot and finally found a solution to detect touch in a screen after 2 days.

我尝试了很多,终于找到了一个解决方案来在 2 天后检测屏幕上的触摸。

Kotlin:

科特林:

If you are having bottom navigation bar and you want to hide on touch...try this!

如果您有底部导航栏并且您想在触摸时隐藏...试试这个!

Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

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

回答by Flash

//on finger touch view visible. On finger up gone

//在手指触摸视图上可见。在手指上消失了

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