2012-05-20 92 views
1

我的應用程序創建一個AlertDialog,其中用戶輸入名稱進行保存。當用戶點擊保存按鈕時,onClickListener將檢查重複的名稱。如果名稱已經存在,將彈出另一個對話框來提醒用戶現有的數據將被替換。然後用戶可以選擇取消並返回更改爲新名稱或繼續進行數據替換。
當第二個對話框出現時,我希望第一個對話框仍然可見,直到我叫解僱。但是,第一個AlertDialog在第二個AlertDialog出現之前消失。當按鈕被點擊時,這將被自動調用。這是一個錯誤還是設計?
我寫了下面的測試用例,我檢查了3個設備:Nexus S Android 4.0,HTC Rezound android 2.3和Motorola Droid Bionic android 2.3。AlertDialog這是一個錯誤?

佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/message" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Some message will be here" 
    /> 

    <Button 
     android:id="@+id/show_btn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Show" 
    /> 

</LinearLayout> 

代碼

public class AlertDialogBug extends Activity 
{ 
    static final int DIALOG_ALERT_ID = 1; 
    AlertDialog alertDlg; 
    TextView message; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 

     message = (TextView) findViewById(R.id.message); 
     Button showButton = (Button) findViewById(R.id.show_btn); 
     showButton.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       showDialog(DIALOG_ALERT_ID); 
      } 
     }); 
    } 

    private AlertDialog createAlertDialog() 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Bug?"); 

     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
     { 

      @Override 
      public void onClick(DialogInterface dialog, int which) 
      { 
       // No dismiss, cancel, finish, or removeDialog, 
       // but the dialog will disappear when this button is clicked. 
      } 

      }); 

     alertDlg = builder.create(); 
     alertDlg.setOnDismissListener(new OnDismissListener() 
     { 

      @Override 
      public void onDismiss(DialogInterface dialog) 
      { 
       message.setText("onDismiss was called"); 
      } 

      }); 

      return alertDlg; 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) 
    { 
     switch (id) 
     { 
      case DIALOG_ALERT_ID: 
       return createAlertDialog(); 

      default: 
       return super.onCreateDialog(id); 
     }  
    } 

} 

我原來寫的保存對話框,與Android的活動:主題= 「@安卓風格/ Theme.Dialog」。 UI在Nexus S和Rezound上看起來很好,但在Droid Bionic上看起來很糟糕(編輯框和按鈕僅佔用一半寬度,另一半則爲空白)。

回答

3

這是設計。 如果你不想通過點擊按鈕取消對話框,下面是一些代碼給你。 當你不想取消對話框時,在你的setPositiveButton方法中添加它。

try { 
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); 
field.setAccessible(true); 
field.set(dialog, false); 

} catch (Exception e) { 
e.printStackTrace(); 
} 

這時如果你想取消對話框,只需在下面添加此。

try { 
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); 
field.setAccessible(true); 
field.set(dialog, true); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

順便說一下,您的XML不會被您的警報對話框調用。由於警報對話框提供了setTitle()和setMessage方法。

如果要提供自定義對話框,請調用setCustomeView(layout)。

任何問題,讓我知道。

+1

謝謝,這個工程,它可以節省我重寫我的應用程序。上面的佈局是針對活動而不是提示對話框的。我的投票被拒絕了,因爲我沒有足夠的聲望,否則你會得到2票。 –

1

不是一個錯誤 - 在這種情況下,只提供了onClick(),因此您可以在單擊按鈕時更新UI /執行操作。

如果您想在單擊「確定」按鈕時保留對話框,您可以嘗試使用確定/取消按鈕創建具有透明背景的自定義活動。

+0

我確實將保存對話框寫爲活動。我想我必須回到佈局,看看我做錯了什麼,使Droid仿生看起來很奇怪。無論如何,如果是通過設計,那麼在onClick中調用dismiss是多餘的,不是嗎? –

+0

正確,您不應該從聽衆打電話解僱。如果用戶在顯示對話框時點擊「後退」按鈕,onDismiss可能會被單獨調用。 – theelfismike