什么是当屏幕旋转时重新创建AlertDialog的最佳方式? [英] What is the best way to recreate an AlertDialog when the screen is rotated?

查看:289
本文介绍了什么是当屏幕旋转时重新创建AlertDialog的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是AlertDialog来创建我的应用程序一个简单的对话框。在code我使用看起来像这样:

  AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setCancelable(假);
builder.setTitle(DIALOGTITLE);
builder.setItems(R.array.options,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
    切换(这){
        情况下0:
            重置();
        打破;
        情况1:
        出口();
        打破;
        默认:
        打破;
       }
    }
});
builder.show();

我已阅读,最好的选择可能是创建一个扩展DialogFragment一个类,然后用我的DialogFragment而不是我目前执行的。

任何人都可以证实,这是最好的解决办法,提出更好的东西,并可能给我一个例子?

感谢您。


解决方案

 公共类ListDialogFragment扩展DialogFragment
    {
    //使用接口的这个实例提供操作事件
    私人listDialogListener mListener;
    私人字符串称号;
    私人诠释物品;    / **
     *创建EndGameDialogFragment,字符串DIALOGTITLE的新实例
     *和ING dialogListItems作为参数。
     * /
    静态的newInstance ListDialogFragment(字符串DIALOGTITLE,诠释dialogListItems)
    {
        ListDialogFragment FRAG =新ListDialogFragment();        //供应NUM输入作为参数。
        捆绑ARGS =新包();
        args.putString(称号,DIALOGTITLE);
        args.putInt(项目,dialogListItems);
        frag.setArguments(参数);        返回FRAG;
    }
    / *创建此对话框片段必备的一个实例活动
     *为了接收事件回调实现此接口。
     *每个方法传递的情况下,主机需要查询它DialogFragment。 * /
    公共接口listDialogListener
    {
        公共无效onDialogClick(DialogFragment对话,诠释它);
    }    //覆盖的Fragment.onAttach()方法来实例化NoticeDialogListener
    @覆盖
    公共无效onAttach(活动活动)
    {
        super.onAttach(活动);
        //验证主机活动实现回调接口
        尝试
        {
            //实例化NoticeDialogListener,所以我们可以将事件发送到主机
            mListener =(listDialogListener)活性;
        }
        赶上(抛出ClassCastException E)
        {
            //活动不实现接口,抛出异常
            抛出新ClassCastException异常(activity.toString()
                    +必须实现NoticeDialogListener);
        }
    }    @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState)
    {
        标题= getArguments()的getString(题)。 // retrive的titleString
        项目= getArguments()调用getInt(项目); // retrive项目数组列表(从strings.xml中)        //建立对话
        AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
        builder.setTitle(职称);
        builder.setItems(项目,新DialogInterface.OnClickListener()
        {
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                开关(所)
                {
                    情况下0:
                        mListener.onDialogClick(ListDialogFragment.this,其中);
                        打破;
                    情况1:
                        mListener.onDialogClick(ListDialogFragment.this,其中);
                        打破;
                    默认:
                        打破;
                }
            }
        });
        返回builder.create();
    }}

I am currently using an AlertDialog to create a simple dialog in my application. The code I am using looks like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(DialogTitle);
builder.setItems(R.array.options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    switch (which) {
        case 0:
            reset();
        break;
        case 1:
        exit();
        break;
        default:
        break;
       }
    }
});
builder.show();

I have read that the best option may be to create a class that extends DialogFragment and then use my DialogFragment instead of my current implementation.

Can anyone confirm that this is the best solution, suggest something better and possibly give me an example?

Thank you.

解决方案

public class ListDialogFragment extends DialogFragment 
    {
    // Use this instance of the interface to deliver action events
    private listDialogListener mListener;
    private String  title;
    private int items;

    /**
     * Create a new instance of EndGameDialogFragment, String dialogTitle
     * and ing dialogListItems as an argument.
     */
    static ListDialogFragment newInstance(String dialogTitle, int dialogListItems) 
    {
        ListDialogFragment frag = new ListDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putString("title", dialogTitle);
        args.putInt("items", dialogListItems);
        frag.setArguments(args);

        return frag;
    }


    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface listDialogListener 
    {
        public void onDialogClick(DialogFragment dialog, int which);
    }

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try 
        {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (listDialogListener) activity;
        } 
        catch (ClassCastException e) 
        {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        title = getArguments().getString("title"); //retrive the titleString
        items = getArguments().getInt("items"); //retrive array of items for the list (from strings.xml)

        //build the dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setItems(items, new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                switch(which)
                {
                    case 0:
                        mListener.onDialogClick(ListDialogFragment.this, which);
                        break;
                    case 1:
                        mListener.onDialogClick(ListDialogFragment.this, which);
                        break;
                    default:
                        break;
                }
            }
        });
        return builder.create();
    }

}

这篇关于什么是当屏幕旋转时重新创建AlertDialog的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆