android - AlertDialog 没有关闭

标签 android android-alertdialog dismiss

我正在制作一个小游戏,我试图显示一个 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/

相关文章:

android - 为android设置一个pc蓝牙服务器

java - 在Android中获取html文本区域的值

android - 如何防止 AlertDialog 关闭?

android - 通知未被取消 (Android)

ios - dismissViewControllerAnimated 忽略过渡样式

android - 无法关闭我的 Android 应用程序中的对话框

java - 如何缩小sqlite数据库?

android - Laplacian OpenCV Android无法正常工作

android - 如何处理自定义警报对话框的外部点击?

android - 如何初始化选择每个选项的多项选择警报对话框?