2012-01-31 55 views
4

我嘗試創建AlertDialog。它工作,但setMessage不刷新消息。下面的代碼片段:onCreateDialog不刷新信息

@Override 
    protected Dialog onCreateDialog(int id) { 
     super.onCreateDialog(id); 
     switch (id) { 
      case CONFIRM:      
       confirmView = new LinearLayout(this); 

       return new AlertDialog.Builder(this) 
         .setIcon(android.R.drawable.ic_dialog_info) 
         .setView(confirmView) 
         .setCancelable(false) 
         .setTitle("The WiFi")      
         .setMessage(infoMsg); 
         .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           Functionality.StartWiFiManager(ControllerService.this); 
          } 
         })       
         .setNegativeButton("Cancel", new OnClickListener() {        
          @Override 
          public void onClick(DialogInterface dialog, int which) {          
          } 
         }) 
         .create(); 
     } 
} 

調用:

infoMsg = "My message";  
showDialog(CONFIRM); 

所以,當我改變我的infoMsg和再次調用的ShowDialog,該消息是一樣的。 我在做什麼錯?請幫幫我。

回答

2

在documenation爲protected Dialog onCreateDialog (int id, Bundle args)它說:

如果使用showDialog(int),活動將通過第一次調用此方法,然後掛到它。任何由此方法創建的對話框都會自動保存併爲您恢復,包括是否顯示。

如果您希望爲您管理保存和恢復對話框的活動,您應該重寫此方法並處理傳遞給showDialog(int)的任何ID。

所以它看起來只會創建一個對話框,然後android會保存/恢復它。

+0

他們應該強調「活動只會第一次調用這個方法」,我總是發現他們的文檔很混亂。 xD – GMsoF 2012-11-01 06:45:38

10

您需要使用'onPrepareDialog'重置每次顯示對話框時都會更改的信息。 onCreateDialog只被調用一次,然後對話框保持並重新使用。每次顯示對話框時調用onPrepareDialog

當然,onCreateDialog和onPrepareDialog都被棄用,你應該使用'DialogFragment'類和'FragmentManager'來代替。但是如果你(像我)繼續使用舊的API,那麼onPrepareDialog就是你想要的。

+0

我已經試過了!我從onCreateDialog除去setMessage和放置在此一進onPrepareDialog象下面這樣:@覆蓋 \t保護無效onPrepareDialog(INT ID,對話的對話){ \t開關(ID){ \t情況CONFIRM: \t((AlertDialog)對話) .setMessage(infoMsg); \t \t break; \t} \t}但它還沒有工作。 – Nolesh 2012-01-31 14:34:32

2

我有同樣的問題,請致電removeDialogDialog

1

,最好不要使用做到無論onCreateDialog()創建Diaog然後用onPrepareDialog()任何茶點或數據的更新時Dialog再次clicked.For這些方法後,您對Dialog沒有太多控制權,並且會增加您的代碼複雜性和冗長,並且通常很難理解。爲此,您可以在xml中創建自己的自定義佈局,並在對話框中將其顯示出來。只需編寫一個方法即可。無需編寫onPrepareDialog()來刷新數據。 這裏是定製Dialog的簡單例子。

這裏您的對話框包含一個帶按鈕名稱的輸入文本框ok取消

mydialog.xml

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    <EditText 
    android:id="@+id/myInput" 
    android:layout_width="fill_parent" 
    android:layout_height="warp_content"/> 
    <Button 
    android:id="@+id/okButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="OK"/> 
    <Button 
    android:id="@+id/cancelButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="cancel"/> 
    </LinearLayout> 

,那麼你必須只寫你的對話框方法

AlertDialog dialog; 
public void myDialog(){ 

AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("Dialog Demo"); 
LayoutInflater inflater=getLAyoutInflater(); 
final View dialogView=inflater.inflate(R.layout.myDialog,null); 
builder.setView(dialogView); 
dialog=builder.create(); 
dialog.show(); 
} 

使用這種方法的任何地方作爲一個對話那麼簡單。