2012-04-29 54 views
2

我正在做一个小游戏,我试图表现出的AlertDialog,问题是,当用户点击一个选项AlertDialog不会dissapear。AlertDialog不解雇

我正在使用loopers,所以我的游戏线程在我的AlertDialog出现时停止(looper),当用户单击“取消”时游戏线程再次运行,但对话框在顶部,用户无法保持播放。

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();  

} 

回答

0

不知道它是否会帮助,但尝试

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(); 
+0

还是同样的问题,感谢您试用 – user1364649

+0

我敢肯定这工作,但我认为我的问题是与我的looper – user1364649

0

它应该是alertadd.dismiss();。而不是dlg.dismiss();

尝试。如果不是让我知道...

+0

没有运气,“该方法dismiss()是不确定的AlertDialog.Builder类型” – user1364649

0

你必须首先创建警报对话框,然后用它在的onClick方法解雇()方法:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
AlertDialog alert = builder.create(); 
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) { 
    alert.dismiss(); 
} 
}); 
alert.show()