如下图所示,如何通过单次触摸获得不同视图的触摸事件。
我试过
private void showAlert() {
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.temp_view);
Button btnOne = (Button) dialog.findViewById(R.id.btnOne);
Button btnTwo = (Button) dialog.findViewById(R.id.btnTwo);
Button btnThree = (Button) dialog.findViewById(R.id.btnThree);
btnTwo.setFocusableInTouchMode(true);
btnTwo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("Down", ">>>>>>>>>>>>>button Two Down <<<<<<<<<<<<<<<");
break;
case MotionEvent.ACTION_UP:
Log.e("Down", ">>>>>>>>>>>>>button Two UP <<<<<<<<<<<<<<<");
break;
}
return false;
}
});
btnThree.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("Down", ">>>>>>>>>>>>>button 3 Down <<<<<<<<<<<<<<<");
break;
case MotionEvent.ACTION_UP:
Log.e("Down", ">>>>>>>>>>>>>button 3 UP <<<<<<<<<<<<<<<");
break;
}
return false;
}
});
btnOne.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("Down", ">>>>>>>>>>>>>button One Down <<<<<<<<<<<<<<<");
break;
case MotionEvent.ACTION_UP:
Log.e("Down", ">>>>>>>>>>>>>button One UP <<<<<<<<<<<<<<<");
break;
}
return false;
}
});
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TempTwitterTimelineActivity.this, "One", Toast.LENGTH_SHORT).show();
}
});
View v = dialog.getCurrentFocus();
if (v != null)
switch (v.getId()) {
case R.id.btnOne:
Toast.makeText(TempTwitterTimelineActivity.this, "One", Toast.LENGTH_SHORT).show();
break;
case R.id.btnTwo:
Toast.makeText(TempTwitterTimelineActivity.this, "Two", Toast.LENGTH_SHORT).show();
break;
case R.id.btnThree:
Toast.makeText(TempTwitterTimelineActivity.this, "Three", Toast.LENGTH_SHORT).show();
break;
}
dialog.show();
}
但只有当我重新触摸其他按钮时它才有效。
我有一个带有按钮的活动,在按钮触摸事件上打开一个自定义对话框,在自定义对话框中我有三个按钮喜欢、分享和查看我想调用与按钮关联的函数,即查看配置文件但其他第二次触摸,如 MotionEvent.ACTION_UP : 是火我关闭对话框。
这是打开对话框代码
txtTemp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (dialog != null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
break;
}
return false;
}
});
为了清楚地了解这里是第二张图片,我在其中启用了开发人员选项中的显示触摸并截取屏幕截图。