将AlertDialog正面按钮文本设置为粗体

时间:2013-12-26 07:29:21

标签: android android-alertdialog android-dialog

这是我的代码。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View vi = inflater.inflate(R.layout.group_dialog_layout,null);
    builder.setView(vi);
    TextView txtNewGroupEntry = (TextView) vi.findViewById(R.id.txtGroupRename);
    if(isNew==true){
        builder.setTitle("New Group");
        txtNewGroupEntry.setText(R.string.new_group_instruction);
    }
    builder.setPositiveButton(R.string.ok_button, null);
    builder.setNegativeButton(R.string.cancel_button, null);
    AlertDialog dialog = builder.create();
    dialog.show();
    Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

我有一个带有添加按钮和取消按钮的警告对话框。我希望按钮的文本都是粗体和斜体。我该怎么办?

3 个答案:

答案 0 :(得分:15)

此示例代码将帮助您......

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("message").setTitle("title");
    AlertDialog alertDialog = builder.create();
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                // do work
                break;
            case DialogInterface.BUTTON_NEUTRAL:
                // do work
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                // do work
                break;
            default:
                break;
            }
        }
    };
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", listener);
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", listener);
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Cancel",
            listener);
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            AlertDialog alertDialog = (AlertDialog) dialog;
            Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);

            button = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);

            button = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);
        }
    });
    alertDialog.show();

答案 1 :(得分:14)

不是将按钮文字设置为"add",而是将其设置为Html.fromHtml("<b><i>add</i></b>")

所以使用你的代码:

更改这些行:

builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);

到这些方面:

builder.setPositiveButton(Html.fromHtml("<b><i>" + getString(R.string.ok_button) + "</i><b>"), null);
// and 
builder.setNegativeButton(Html.fromHtml("<b><i>" + getString(R.string.cancel_button) + "</i><b>"), null);

您可以修改strings.xml文件中的字符串。

例如,如果你的字符串看起来像这样:

<string name="ok_button">add</string>
<string name="cancel_button">cancel</string>

您可以将其更改为:

<string name="ok_button"><b><i>add</i></b></string>
<string name="cancel_button"><b><i>cancel</i></b></string>

您仍然错误地引用了Strings资源。而不是R.string.ok_button,因为它返回一个int,你必须使用getString(R.string.ok_button)

所以你必须改变这些行:

builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);

到这些方面:

builder.setPositiveButton(getString(R.string.ok_button), null);
// and
builder.setNegativeButton(getString(R.string.cancel_button), null);

答案 2 :(得分:8)

您可以访问Dialog's中的buttons onShow(),就像我在下面提到的那样。在onShow()触发时,您可以访问yoir对话框的UI,试试这个,

private void showAlertDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setMessage("Dailog Demo!");

    builder .setCancelable(false)
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           dialog.cancel();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           dialog.cancel();
       }
   });

    final AlertDialog alertdialog = builder.create();
    alertdialog.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            ((Button)alertdialog.getButton(Dialog.BUTTON_POSITIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
            ((Button)alertdialog.getButton(Dialog.BUTTON_NEGATIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
        }
    });
    alertdialog.show();
}