![](/img/trans.png)
[英]How do I detect touch input on the Android when application is running in the background
[英]How do I detect touch input on the Android
現在我要做的就是檢測何時按下屏幕,然后顯示一條日志消息以確認它發生了。 到目前為止,我的代碼是根據 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()。 有關詳細信息,請參閱文檔。
這是一個關於如何檢測簡單的觸摸事件、獲取坐標和顯示吐司的簡單示例。 此示例中的事件是向下操作、移動和向上操作。
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;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.