AlertDialog这是一个错误吗?

时间:2012-05-20 00:47:08

标签: android

我的应用会创建一个AlertDialog,用户在其中输入要保存的名称。当用户单击“保存”按钮时,onClickListener将检查重复的名称。如果名称已存在,则会弹出另一个对话框,提醒用户现有数据将被替换。然后,用户可以选择取消并返回更改为新名称或继续更换数据 当第二个对话框出现时,我希望第一个对话框仍然可见,直到我调用dismiss。但是,第一个AlertDialog在第二个AlertDialog出现之前消失了。也就是说,单击按钮时会自动调用dismiss。这是一个错误还是设计? 我在下面编写了测试用例,我检查了3个设备:Nexus S android 4.0,HTC Rezound android 2.3和Motorola Droid Bionic android 2.3。

布局

<?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 =“@ android:style / Theme.Dialog”将保存对话框编写为活动。在Nexus S和Rezound上用户界面看起来很好,但在Droid Bionic看起来很糟糕(编辑框和按钮只占宽度的一半,另一半是空白)。

2 个答案:

答案 0 :(得分: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方法由alert对话框提供。

如果要提供自定义对话框,请调用setCustomeView(layout)。

有任何问题,请告诉我。

答案 1 :(得分:1)

不是错误 - 在这种情况下,只提供onClick(),以便您可以在单击按钮时更新UI /执行操作。

如果要在单击“确定”按钮时保留对话框,可以尝试使用带有“确认/取消”按钮的透明背景创建自定义活动。