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

后端 未结 8 1715
挽巷
挽巷 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:43

    Sometimes completely different perspectives are the solution to a problem, the HI-user culture.

    • In Win32 an OK and a Cancel button is absolutely necessary or the dialog will not work.
    • In Android the HI is quite different because of the existence of the OS "Back" button (onBackPressed() that could also be overridden, ESC on attached physical keyboards) in the operative system HI.

      • There is no need for a Negative/cancel button in most cases.
      • So most dialog are enough with the Positive button
      • Then there are no button order issue, the positive button is alone?
      • If you are in an edit field set it to edittext.setSingleLine(); and enter will move focus to the single positive button. Enter again and its positive button pressed.
      • If the user wants to push Cancel, he presses the OS "Back" button (As a developer we do not need to explain the core of the OS HI policy to the user, that is an issue of the OS issuer. We just leave the Android dialog without a Cancel button.)
    0 讨论(0)
  • 2021-02-05 06:45

    this is my solution. It is work for me.

        // Show alertDialog after building
        AlertDialog alertDialog = createAlertDialog(context);
        alertDialog.show();
        // and find positiveButton and negativeButton
        Button positiveButton = (Button) alertDialog.findViewById(android.R.id.button1);
        Button negativeButton = (Button) alertDialog.findViewById(android.R.id.button2);
        // then get their parent ViewGroup
        ViewGroup buttonPanelContainer = (ViewGroup) positiveButton.getParent();
        int positiveButtonIndex = buttonPanelContainer.indexOfChild(positiveButton);
        int negativeButtonIndex = buttonPanelContainer.indexOfChild(negativeButton);
        if (positiveButtonIndex < negativeButtonIndex) {
            // prepare exchange their index in ViewGroup
            buttonPanelContainer.removeView(positiveButton);
            buttonPanelContainer.removeView(negativeButton);
            buttonPanelContainer.addView(negativeButton, positiveButtonIndex);
            buttonPanelContainer.addView(positiveButton, negativeButtonIndex);
        }
    
    0 讨论(0)
提交回复
热议问题