本文介紹了Android 對話框自行消失的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用以下代碼來創建自己的對話框:
I'm using the following code to create my own dialog:
public void ShowMessageDialog(String str){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(str);
builder.setCancelable(false);
builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
它工作正常,但在此函數中使用時,對話框會自行消失:
It works fine but it appears the Dialog disappears on it's own when used inside this function:
public void test(String str){
ShowMessageDialog("About to start new activity");
Intent intent = new Intent(this,PageViewer.class);
startActivity(intent);
}
似乎新活動已創建并且顯然擺脫了對話框.但為什么?活動不應該在打開新活動之前停止嗎?
It seems that the new activity is created and obviously gets rid of the dialog. But why? Shouldn't the activity stop before opening the new one?
謝謝!
推薦答案
即將觸發的 Intent 不會等待您的對話框被取消.因此,在顯示對話框后,新的活動就開始了.你可以像這樣完成你想要的:
Intent which is about to fire doesn't wait for your dialog to be canceled. So, right after dialog is shown, new Activity is started. You could accomplish what you want like this:
public void ShowMessageDialog(String str){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(str);
builder.setCancelable(false);
builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(this,PageViewer.class);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void test(String str){
ShowMessageDialog("About to start new activity");
}
這篇關于Android 對話框自行消失的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!