我正在制作一个小游戏,我试图显示一个 AlertDialog,问题是,当用户单击一个选项时,AlertDialog 不会消失。
我正在使用循环程序,所以当我的 AlertDialog 出现时我的游戏线程停止(循环程序),当用户单击“取消”时游戏线程再次运行但对话框在顶部并且用户无法继续玩。
public void alerta(){
Looper.prepare();
myHandler = new Handler();
AlertDialog.Builder alertadd = new AlertDialog.Builder(_context);
LayoutInflater factory = LayoutInflater.from(_context);
final View view = factory.inflate(R.layout.custom_dialog, null);
alertadd.setView(view);
alertadd.setNeutralButton("Tomar Foto", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
stops++;
Toast.makeText(_context.getApplicationContext(), "Tomaste Foto", Toast.LENGTH_SHORT).show();
}
});
alertadd.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
System.out.println("Stops = "+stops);
stops++;
Toast.makeText(_context.getApplicationContext(), "Cancelar", Toast.LENGTH_SHORT).show();
myHandler.getLooper().quit();
dlg.dismiss();
}
});
alertadd.show();
Looper.loop();
}
最佳答案
不确定是否有帮助,但试试
dlg.cancel();
或者看看这个。在我的情况下它工作得很好:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.a))
.setCancelable(false)
.setPositiveButton(getString(R.string.b), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton(getString(R.string.c), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
关于android - AlertDialog 没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375745/