如何在AlertDialog中加粗文本?

时间:2015-04-16 15:53:30

标签: android

有一个AlertDialog

public class MessageDialogView extends AlertDialog {

    private View contenu, titleBar;

    @SuppressLint("InlinedApi")
    public MessageDialogView(Context context, LayoutInflater inflater) {
        super(context, AlertDialog.THEME_HOLO_DARK);
        contenu = inflater.inflate(R.layout.msg_dialog, null);
        titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
        setCustomTitle(titleBar);
        setView(contenu, 0, 0, 0, 0);
        setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_ok), new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }
    public void setIcone(int resId) {
        ((ImageView)titleBar.findViewById(R.id.icone)).setImageResource(resId);
    }
    public void setTitre(String titre) {
        if (titre != null)
            ((TextView)titleBar.findViewById(R.id.titre)).setText(titre);
    }
    public void setMsg(String text){
        if (text != null)
            ((TextView)contenu.findViewById(R.id.msgText)).setText(text);
    }
}

布局msg_dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    style="@style/ImpotsStyle" >
        <TextView
            android:id="@+id/msgText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/ImpotsStyleTransparent"
            android:gravity="center"
            android:paddingTop="35dp"
            android:paddingBottom="35dp" />
</LinearLayout>

布局custom_dialog_title:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ImpotsTitleStyle"
    android:layout_marginBottom="0dp"
    android:padding="0dp">
    <ImageView
        android:id="@+id/icone"
        android:src="@drawable/ic_action_warning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/descr_alert_icone"/>
    <TextView 
        android:id="@+id/titre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        style="@style/ImpotsStyleText"/>
</RelativeLayout>

我想在AlertDialog

中显示此activity
...
private MessageDialogView dlg = null;
private String champOblig = "";
...
@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.parcelle);

        db = new Db(ParcelleActivity.this).open();

        afficherParcelle();

        dlg = new MessageDialogView(ParcelleActivity.this, getLayoutInflater());

    }
public void submit(View vue) { // onClick of a button

        if (chpObligVide()) {
            displaychpObligError();
            return;
        }
        ...
}
private boolean chpObligVide() {

        if (TextUtils.isEmpty(identification.getText().toString())) {
            champOblig = Formatage.bold(getResources().getString(R.string.identification)); // here I want to make the text to be bold
            return true;
        }
        champOblig = "";
        return false;
    }
    private void displaychpObligError() {
        dlg.setTitre(getResources().getString(R.string.titreErrMsgBox));
        dlg.setMsg(getResources().getString(R.string.chpOblig) + " " + champOblig);
        dlg.show();
    }

类格式:

public class Formatage {

    public Formatage() {
        super();
    }
    public static String bold(String text) {

        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        ssb.setSpan(boldSpan, 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return ssb.toString();

    }

}

那么如何在bold内显示文字AlertDialog

2 个答案:

答案 0 :(得分:1)

如果您使用SpannableStringBuilder,则无法使用toString(),否则您将丢失刚刚添加的粗体格式。

将方法粗体更改为返回SpannedString

public class Formatage {

    public Formatage() {
        super();
    }

    public static SpannedString bold(String text, String boldText) {
        int start = text.length() + 1;

        SpannableStringBuilder ssb = new SpannableStringBuilder(text + " " + boldText);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        ssb.setSpan(boldSpan, start, start + boldText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return new SpannedString(ssb);
    }
}

现在您需要更改活动方法以支持新的粗体方法:

private boolean chpObligVide() {
    if (TextUtils.isEmpty(identification.getText().toString())) {
        champOblig = getResources().getString(R.string.identification)); 
        return true;
    }
    champOblig = "";
    return false;
}

private void displaychpObligError() {
    dlg.setTitre(getResources().getString(R.string.titreErrMsgBox));
    dlg.setMsg(Formatage.bold(getResources().getString(R.string.chpOblig), champOblig));
    dlg.show();
}

最后编辑MessageDialogView以接受CharSequence而不是String:

public void setMsg(CharSequence text) {
    if (text != null)
        ((TextView)contenu.findViewById(R.id.msgText)).setText(text);
}

答案 1 :(得分:0)

基本上,您可以在xml或代码中设置“粗体”样式。

Set TextView style (bold or italic)

如果您使用代码,我会这样做,

public void setMsg(String text){
    if (text != null){
        TextView textView = ((TextView)contenu.findViewById(R.id.msgText));
        textView.setText(text);            
        textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    }
}

在xml中,

<TextView
    android:id="@+id/msgText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/ImpotsStyleTransparent"
    android:gravity="center"
    android:textStyle="bold"
    android:paddingTop="35dp"
    android:paddingBottom="35dp" />