为什么一个人应该几乎总是通过onCreateDialog显示警告对话框

时间:2012-05-24 03:13:05

标签: android android-alertdialog

几天前,我问了一个关于提醒对话的问题AlertDialog is this a bug?在提出问题之前,我经历了一些关于提醒对话的帖子。在我读到的所有问题上,都使用show()来显示对话框。我不知道提问者是否真的在他们的应用程序上使用发布的代码。如果它们是,那么除非活动仅为纵向或横向,否则它们应使用showDialog(int id)显示对话框。这样做的原因是,只有在使用showDialog时,操作系统才会为您进行方向更改。这在下面的代码中说明。只需单击按钮,然后旋转设备即可查看将要发生的情况。

布局

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

    <Button
        android:id="@+id/with_show_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"
        android:clickable = "true"
        android:text="With Show"
    />

    <Button
        android:id="@+id/no_show_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/with_show_btn"
        android:layout_alignParentBottom="true"
        android:clickable = "true"
        android:text="No Show"
    />

    <Button
        android:id="@+id/no_on_create_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/no_show_btn"
        android:layout_alignParentBottom="true"
        android:clickable = "true"
        android:text="Not Using onCreateDialog"
    />

</RelativeLayout>  

代码

public class AlertAndShowActivity extends Activity implements OnClickListener
{
    static final int DIALOG_SHOW_ID = 1;
    static final int DIALOG_NO_SHOW_ID = 2;
    static final int DIALOG_NOT_USING_ONCREATEDIALOG_ID = 3;

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

        setContentView(R.layout.main);

        Button showBtn = (Button) findViewById(R.id.with_show_btn);
        showBtn.setOnClickListener(this);

        Button noShowBtn = (Button) findViewById(R.id.no_show_btn);
        noShowBtn.setOnClickListener(this);

        Button notUsingOnCreateDialogBtn = (Button) findViewById(R.id.no_on_create_btn);
        notUsingOnCreateDialogBtn.setOnClickListener(this);
    }

    protected AlertDialog createDialog(final int id)
    {       
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dismiss is not enough");

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

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // When dialog is showing through showDialog(int id)
                // and show() is called, then calling cancel will dismiss 
                // the dialog but it will reappear on orientation change.
                // When you run this, chick WithShow and then click Cancel 
                // and rotate your device to see what I mean.
                dialog.cancel();
            }
        });

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

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // system will automatically call dismiss
                // see also remark for the cancel case above.
            }
        });

        builder.setNeutralButton("Good", new DialogInterface.OnClickListener()
        {
                        @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // This will produce the behavior that one wants when show() 
                // is used in the code. That is the dialog will not reappear
                // on orientation change.
                removeDialog(id);   
            }
        });

        return builder.create();
    }

    protected AlertDialog createDialogWithShow(int id)
    {   
        AlertDialog alertDlg = createDialog(id);
        // Only call show if you want to disable a button.
        // You can only disable a button after calling show(),
        // otherwise you will get null pointer exception.
        alertDlg.show();
        //alertDlg.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);

        return alertDlg;
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            // notUsingOnCreateDialogBtn 
            case R.id.no_on_create_btn:
                // this will create the dialog and show it on the UI
                // but if one rotates the device it will not be
                // automatically recreated.
                createDialogWithShow(DIALOG_NOT_USING_ONCREATEDIALOG_ID);
                break;

            // withShowBtn
            case R.id.with_show_btn:
                // OS will recreate the dialog on orientation change.
                showDialog(DIALOG_SHOW_ID);
                break;

            case R.id.no_show_btn:
                showDialog(DIALOG_NO_SHOW_ID);
        }
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        // no case here for DIALOG_NOT_USING_ONCREATEDIALOG_ID
        // since it is not shown using showDialog
        switch (id)
        {
            case DIALOG_NO_SHOW_ID:
                return createDialog(id);

            case DIALOG_SHOW_ID:
                return createDialogWithShow(DIALOG_SHOW_ID);

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

}

0 个答案:

没有答案