当我按下屏幕时显示alertdialog



我有一个调查列表,所有这些调查都包含一张图片。当你完成一项调查时,应用程序会返回到概览,在那里我可以看到所有已完成和未完成的调查以及所有被拒绝的调查。

我想要什么:

现在,如果我在屏幕上长按,调查没有被拒绝,并且已经完成,我想只要按下屏幕就可以预览图片,如果我释放屏幕,就会消失。

问题是:

  • 如何在不添加阳性的情况下取消或取消AlertDialog按钮

  • 是否可以在没有onTouch Listener的情况下获取MotionEvent,以及只使用longClickListener?

这是我的onTouch和longClick监听器的代码,我想消除其中一个:

row.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
row.setOnLongClickListener(l -> {
// V3.0 only execute long click if the survey hasn't been declined
if (getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && !erhebung.isCompleted()) {
((SurveyListPatientActivity) getContext()).showRejectSurvey(erhebung);
}else if(getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && erhebung.isCompleted()){
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
adb.create().show();
if (event.getAction() == MotionEvent.ACTION_UP){
// cancel AlertDialog
}
}
return true;
});
return false;
}
});

非常感谢!

编辑:

在@brianoqr的帮助下,我把代码改成了这个。一切正常,但对话框并没有消失。

row.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
isLongPressed = true;
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});

我可能会采取不同的做法,但为了回答如何取消没有按钮的对话框的问题:

AlertDialog dialog = adb.create();
dialog.show()
if (event.getAction() == MotionEvent.ACTION_UP){
dialog.dismiss()
}

你写的代码不会像你想的那样工作,长按是主要的动作,你可以在这张票上构建你的代码:Android-检测长按的结束

然后,你的对话框需要在不同的范围内,以允许自动解雇

编辑

row.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(!isLongPressed){
isLongPressed = true;
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
}
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});

最新更新