Extending AlertDialogs in Android (where to find examples, how to get title and buttons)

前端 未结 3 1986
别跟我提以往
别跟我提以往 2020-12-16 07:18

I have been looking a lot for examples on how to correctly extend AlertDialogs and get the expected behaviour, but I can hardly find any.

The docs on google doesnt r

相关标签:
3条回答
  • 2020-12-16 07:25

    There are few options to create custom AlertDialog. I just would like to give you answer for current question. You can set title, message and other components of AlertDialog in onCreate() method. But make sure you doing it before you calling super.onCreate() Example:

    public class PausDialog extends AlertDialog {
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    View content = LayoutInflater.from(getContext()).inflate(R.layout.dialog_some_view, null);
                    setView(content);
                    setTitle("Some Title");
                    setMessage("Some Message");
                    setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           Log.d("TAG", "BUTTON_POSITIVE");
                       }
                    });
                    ((TextView) content.findViewById(R.id.data)).setText(R.string.some_custom_data);
                    ((TextView) content.findViewById(R.id.description)).setText(getContext().getString(R.string.description));
                    setCancelable(false);
                    setOnKeyListener((dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);
    
                    super.onCreate(savedInstanceState);
                }
    }
    
    0 讨论(0)
  • 2020-12-16 07:31

    I've successfully made my own custom AlertDialog by extending the Builder class.

    public class MyDialog extends AlertDialog.Builder {
        private Context mContext;
        private AlertDialog mAlertDialog;
    
        public MyDialog(Context context) {
            super(context);
            mContext = context;
        }
    
        @SuppressLint("InflateParams")
        @Override
        public AlertDialog show() {
            View view = LayoutInflater.from(mContext).inflate(R.layout.my_dialog, null);
    
            ...
            mAlertDialog = super.show();
            return mAlertDialog;
        }
    
    }
    

    Then, from the code, I instantiate them like this:

    MyDialog myDialog = new MyDialog(getActivity());
    myDialog.show();
    

    The only caveat is you have to take care of dismissing the dialog properly on orientation change or other events that don't explicit call the dismiss() method, like the back button or something else. Otherwise, you'll have memory leaks from that dialog.

    0 讨论(0)
  • 2020-12-16 07:37

    I got my custom AlertDialog to work using something similiar to this code.

    First make your constructor publicly accessible

    public PausDialog(Context context)
    

    Then you can simply instantiate and show it as so:

    PauseDialog newDialog = new PauseDialog(this);
    newDialog.setTitle("My Dialog");
    newDialog.setButton("OK", ...); // Insert your onClick implementation
    newDialog.setButton2("Cancel", ...); // Insert your onClick implementation
    newDialog.Show();
    
    0 讨论(0)
提交回复
热议问题