0

几天前,我问了一个关于警报对话框的问题AlertDialog 这是一个错误吗?在问这个问题之前,我浏览了一些关于警报对话框的帖子。在我阅读的所有问题上,都使用 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);
        }       
    }

}
4

0 回答 0