如何加粗AlertDialog中使用的CharSequence中的一项?

时间:2015-02-11 22:09:24

标签: java android alertdialog android-alertdialog

只是一个简单的问题。我有一个警告对话框弹出窗口,为用户提供一个简短的选项列表。

我希望其中一个项目显示为粗体。这是我用于选项列表的基本代码。我想" option1"大胆,所有其他选项定期。有没有办法在不使用自定义文本视图的情况下实现这一目标?

CharSequence options[] = new CharSequence[] {"option1", "option2", "option3"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an option");
builder.setItems(options, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // my actions
    }
});
builder.show();

2 个答案:

答案 0 :(得分:6)

您可以使用SpannableString

String b = "boldOption";
SpannableString boldOption = new SpannableString(b);
boldOption.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, b.length(),0);
CharSequence options[] = new CharSequence[] {boldOption, "option2", "option3"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an option");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    // my actions
}
});
builder.show();

答案 1 :(得分:0)

您可以在选项1中使用Html.fromHtmlHtml.fromHtml("<b>option1</b>")

CharSequence options[] = new CharSequence[] {Html.fromHtml("<b>option1</b>"), "option2", "option3"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick an option");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // my actions
            }
        });
        builder.show();