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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
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;
}
}
回答3:
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 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:
I have tried alot and finally found a solution to detect touch in a screen after 2 days.
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.
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)
}