[英]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.