【发布时间】:2025-12-12 05:00:02
【问题描述】:
现在我要做的就是检测何时按下屏幕,然后显示一条日志消息以确认它发生了。到目前为止,我的代码是根据 CameraPreview 示例代码修改的(它最终会拍照),因此大部分代码位于扩展 SurfaceView 的类中。 SDK 中示例代码的 API 为 7。
【问题讨论】:
标签: android events input touch
现在我要做的就是检测何时按下屏幕,然后显示一条日志消息以确认它发生了。到目前为止,我的代码是根据 CameraPreview 示例代码修改的(它最终会拍照),因此大部分代码位于扩展 SurfaceView 的类中。 SDK 中示例代码的 API 为 7。
【问题讨论】:
标签: android events input touch
尝试下面的代码来检测触摸事件。
mView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//show dialog here
return false;
}
});
要显示对话框,请使用 Activity 方法 showDialog(int)。您必须实现 onCreateDialog()。有关详细信息,请参阅文档。
【讨论】:
View 对象。请参阅this answer 了解如何创建一个。
这是一个简单的示例,说明如何检测简单的触摸事件、获取坐标并显示 toast。此示例中的事件是 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;
}
}
【讨论】:
我是这样做的:
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>
【讨论】:
我尝试了很多,终于在 2 天后找到了检测屏幕触摸的解决方案。
科特林:
如果您有底部导航栏并且想要隐藏触摸...试试这个!
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)
}
【讨论】:
//在手指触摸视图上可见。手指头不见了
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;
}
});
【讨论】: