How can I set the order of the positive and negative buttons in AlertDialog?

后端 未结 8 1740
挽巷
挽巷 2021-02-05 06:05

Why I want to do this is another discussion entirely, but I need to figure out the best way to make all my alert dialogs have the positive button on the right side. Note that i

8条回答
  •  遇见更好的自我
    2021-02-05 06:35

    You can extend the AlertDialog.Builder to force the order:

    public class MyDialog extends AlertDialog.Builder {
    
        public MyDialog(Context arg0) {
            super(arg0);
        }
    
        @Override
        public Builder setPositiveButton(CharSequence text, OnClickListener listener) {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                return super.setNegativeButton(text, listener);
            } else {
                return super.setPositiveButton(text, listener);
            }
    
        }
    
        @Override
        public Builder setNegativeButton(CharSequence text, OnClickListener listener) {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                return super.setPositiveButton(text, listener);
            } else {
                return super.setNegativeButton(text, listener);
            }
        }
    
    }
    

提交回复
热议问题