我有一个调查列表,所有这些调查都包含一张图片。当您完成一项调查后,应用程序会返回到概览,我可以在其中看到所有已完成和所有未完成的调查以及所有被拒绝的调查。
我想要什么:
现在,如果我在屏幕上长按,调查没有被拒绝并完成,我想预览图片,只要我按下屏幕,如果我松开屏幕,它就会消失。
问题是:
如何在不添加肯定的情况下取消或关闭 AlertDialog 按钮?
是否可以在没有 onTouch 监听器的情况下获取 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 的帮助下,我将我的代码改成了这个。 一切正常,但 Dialog dosent 消失了。
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()
}
您编写的代码不会如您所愿地工作,onlongpress 是主要操作,您可以像这张票中的代码那样构建您的代码:Android - Detect End of Long Press
然后您的对话将需要在不同的范围内以允许自动关闭
编辑
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;
}
});
关于java - 当我按下屏幕时显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58858150/