添加多种颜色以警告对话框消息

时间:2014-02-18 22:51:16

标签: android android-layout alertdialog

我有一个AlertDialog。我想用多种文本颜色键入消息。我在字符串资源中有以下内容。但是消息中的文字仍然显示为全黑。

<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>

我的意思是

myAlert.setMessage(getResources().getString(R.string.some_text)

1 个答案:

答案 0 :(得分:0)

// try this way
1. custom class for set different color for different text.

public class ColorSpannable extends ClickableSpan {

        private int color = -1;
        private float fontSize = -1;
        private boolean isUnderline = true;

        /**
         * Constructor
         */
        public ColorSpannable() {
        }

        /**
         * Constructor
         */
        public ColorSpannable(int color) {
            this.color = color;
        }

        /**
         * Constructor
         */
        public ColorSpannable(float fontSize) {
            this.fontSize = fontSize;
        }

        /**
         * Constructor
         */
        public ColorSpannable(boolean isUnderline) {
            this.isUnderline = isUnderline;
        }

        /**
         * Constructor
         */
        public ColorSpannable(int color, boolean isUnderline) {
            this.isUnderline = isUnderline;
            this.color = color;
        }

        /**
         * Constructor
         */
        public ColorSpannable(int color, float fontSize) {
            this.color = color;
            this.fontSize = fontSize;
        }

        /**
         * Overrides methods
         */
        @Override
        public void updateDrawState(TextPaint ds) {

            if (color != -1) {
                ds.setColor(color);
            }
            if (fontSize > 0) {
                ds.setTextSize(fontSize);
            }

            ds.setUnderlineText(isUnderline);

        }

        @Override
        public void onClick(View widget) {

        }
}

2. custom method for apply different color for different text.

public SpannableStringBuilder addClickablePart(String message,final ArrayList<String> changeTextList,ArrayList<String> changeTextColorList) {

        String str = message;
        SpannableStringBuilder ssb = new SpannableStringBuilder(Html.fromHtml(message));
        for(int i=0;i<changeTextList.size();i++){
            String colorHashValue;
            if(changeTextColorList.size()>=i){
                colorHashValue = changeTextColorList.get(i);
            }else{
                colorHashValue = "#ff0000";
            }
            ssb.setSpan(new ColorSpannable(Color.parseColor(colorHashValue), false) {
                @Override
                public void onClick(View widget) {

                }
            }, str.indexOf(changeTextList.get(i)), str.indexOf(changeTextList.get(i)) + changeTextList.get(i).length(), 0);

        }
            return ssb;
}

3. how to use it

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        ArrayList<String> textList = new ArrayList<String>();
        ArrayList<String> textColorList = new ArrayList<String>();
        textList.add("red");
        textList.add("green");
        textList.add("blue");
        textList.add("yellow");
        textList.add("pink");

        textColorList.add("#ff0000");
        textColorList.add("#008000");
        textColorList.add("#0000ff");
        textColorList.add("#ffff00");
        textColorList.add("#ffc0cb");
        builder.setTitle("Multiple color text Dialog").setMessage(addClickablePart("this my custom text red green blue yellow pink",textList,textColorList)).setCancelable(false).setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
        AlertDialog alert = builder.create();
        alert.setCancelable(true);
        alert.show();

here i gave you demo code you can also modify as per your requirement still any problem let me know will try to help.