[英]Android : Add delay to Alert Dialog
我处于需要几秒钟后弹出警报对话框的情况。 我用postDelayed()尝试了Handler,但是没有用。
new Handler().postDelayed(new
Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(
MyActivity.this);
builder.setTitle("My title... ");
builder. setMessage("my msg..");
builder. setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log. e("info", "OK");
}
});
builder.show();
}
});
}
}, 33000);
您必须在builder.show();下面添加这段代码。 这个对我有用。
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialog.dismiss();
}
}, 1500); // 1500 seconds
编辑:
在您的情况下,我以不同的方式使用它。 在这里您可以看到我的完整代码。 以我为例,我只希望发出“模拟”进度对话框,一段时间后它消失。
protected void ActiveProgressDialog() {
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "", getResources().getString(R.string.carregantInfo), true);
dialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialog.dismiss();
}
}, 1500); // 1500 milliseconds
}
}
我知道这是一个老问题,因为我认为问题可能是您没有从主(UI)线程运行。
确保您的处理程序与主线程关联
new Handler(Looper.getMainLooper()).postDelayed(...);
希望这可以帮助!
private Thread mMainThread;
mMainThread = new Thread(){
@Override
public void run(){
try{
synchronized (this) {
wait(33);
try{
AlertDialog.Builder builder = new AlertDialog.Builder(
MyActivity.this);
builder.setTitle("My title... ");
builder. setMessage("my msg..");
builder. setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log. e("info", "OK");
}
});
builder.show();
}catch(Exception e){
}
}
}catch (Exception e) {
}
}};
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.