我有一个警报对话框,我希望它可以从一个可运行的方法访问。目标是在达到某个标准后取消对话框。
以下是创建AlertDialogue的代码:
public void perm_dialogue()
{
final WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.permission_wifi, null);
perm_prog = (ProgressBar)promptsView.findViewById(R.id.perm_progressBar);
perm_text = (TextView)promptsView.findViewById(R.id.perm_text);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
wifiManager.setWifiEnabled(true);
//call the delay function
handler1.post(DelayTask);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
perm_prog.setVisibility(promptsView.INVISIBLE);
}
现在这是我用来制作Runnable的代码:
public Runnable DelayTask = new Runnable()
{
@Override
public void run()
{
final WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//scanning loop for 10 secs
if(wifiManager.isWifiEnabled()==true)
{
//process
perm_prog.setVisibility(View.INVISIBLE);
//Close Alert Dialogue
//stopHandler();
handler1.removeCallbacks(this);
return;
}
else if(k==40)
{
//stopHandler();
handler1.removeCallbacks(this);
return;
}
else
{
perm_prog.setVisibility(View.VISIBLE);
perm_text.setText("Please Wait...");
}
k++;
handler1.postDelayed(DelayTask, 250);
}
};
现在在runnable中,我已经放置了一个Comment标签,指示我想要调用AlertDialogue的取消功能的地方。
任何Suggession都会有所帮助! 谢谢!
答案 0 :(得分:1)
尝试如下,将AlertDialog作为成员变量
class MyActivity extends Activity{
AlertDialog alertDialog=null;
onCreate(-){
}
}
并按如下所示创建对话框
// create alert dialog
alertDialog = alertDialogBuilder.create();
然后在活动中的任何地方调用dismiss()
alertDialog.dismiss();
希望这会对你有所帮助。
答案 1 :(得分:0)
您可以将alertDialog保留为全局变量,然后调用alertDialog.dismiss(),其中包含注释标记。