What is the best way to recreate an AlertDialog wh

2019-08-15 10:29发布

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.

2条回答
甜甜的少女心
2楼-- · 2019-08-15 11:07
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();
    }

}
查看更多
看我几分像从前
3楼-- · 2019-08-15 11:11

You can override the onConfigurationChanged(Configuration newConfig) method of your activity to recognize the orientation change and restore the AlertDialog if it was open. For this you will need the following tag in your activity definition in your manifest android:configChanges="orientation. A good introduction can be found here.

查看更多
登录 后发表回答