开发者

Stopping Multiline Text for buttons in an AlertDialog

开发者 https://www.devze.com 2023-03-26 12:35 出处:网络
I have a开发者_StackOverflow中文版n alert dialog where users enter text with an OK and a Cancel button. It looks fine when I test on a tablet, but on my phone the Cancel text on the button is broken o

I have a开发者_StackOverflow中文版n alert dialog where users enter text with an OK and a Cancel button. It looks fine when I test on a tablet, but on my phone the Cancel text on the button is broken onto two lines: Can cel

There looks like there is enough room on my phone, but the text is broken up. I know how to fix this on a normal button, but when I tried alert.getButton to get the button the alert stops showing up all together. Here is the code I tried:

Here's my code:

final AlertDialog alert = new AlertDialog.Builder(getActivity()).create();
      final EditText input = new EditText(getActivity());
      input.setText(existingName);
      alert.setView(input);        
      alert.setButton("Ok", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {

              //do stuff here
          }
      });

      alert.setButton2("Cancel",
              new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      dialog.cancel();
                  }
              });
      alert.getButton(1).setMaxLines(1);
      alert.show();


ok change

alert.getButton(1).setMaxLines(1);

to

alert.getButton(DialogInterface.BUTTON2).setMaxLines(1);


The valid parameters for AlertDialog.getButton(Integer) are

DialogInterface.BUTTON1
DialogInterface.BUTTON2
DialogInterface.BUTTON3
DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL
DialogInterface.BUTTON_NEGATIVE

The first 3 are depreciated, so it's recommended you set your button using this method:

public void setButton(int whichButton,
                      CharSequence text,
                      DialogInterface.OnClickListener listener)

Where whichButton is one of the bottom 3 options above.

0

精彩评论

暂无评论...
验证码 换一张
取 消