目前,我只想检测屏幕是否被按下,并显示日志消息以确认它发生了。到目前为止,我的代码是基于CameraPreview示例代码修改而来的(最终将会拍照),因此大部分代码位于扩展了SurfaceView类的类中。SDK示例代码的API版本号为7。
尝试使用以下代码来检测触摸事件。
mView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//show dialog here
return false;
}
});
使用Activity的showDialog(int)方法显示对话框。您必须实现onCreateDialog()。有关详细信息,请参阅文档。
这里有一个简单的示例,展示如何检测简单的触摸事件、获取坐标并显示一个提示信息。该示例中包含三个事件:按下(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 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>
我尝试了很多次,终于在两天后找到了一种解决方案,可以检测屏幕触摸。
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)
}
//当手指触摸视图时可见。当手指抬起时不可见。
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;
}
});