/*
http://blog.csdn.net/harvic880925/article/details/38984705
*参数说明:
object what :对应的各种Span,后面会提到;
int start:开始应用指定Span的位置,索引从0开始
int end:结束应用指定Span的位置,特效并不包括这个位置。比如如果这里数为3(即第4个字符),第4个字符不会有任何特效。从下面的例子也可以看出来。
int flags:取值有如下四个
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE:前后都不包括,即在指定范围的前面和后面插入新字符都不会应用新样式
Spannable.SPAN_EXCLUSIVE_INCLUSIVE :前面不包括,后面包括。即仅在范围字符的后面插入新字符时会应用新样式
Spannable.SPAN_INCLUSIVE_EXCLUSIVE :前面包括,后面不包括。
Spannable.SPAN_INCLUSIVE_INCLUSIVE :前后都包括。
*
*
* span:
* //再构造一个改变字体颜色(ForegroundColorSpan)
span = new ForegroundColorSpan(Color.BLUE);
//改变字背景色(BackgroundColorSpan)
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
//改变字大小(AbsoluteSizeSpan)
AbsoluteSizeSpan span = new AbsoluteSizeSpan(16);
//粗体、斜体(StyleSpan)
StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
//删除线(StrikethroughSpan)
StrikethroughSpan span = new StrikethroughSpan();
//下划线(UnderlineSpan)
UnderlineSpan span = new UnderlineSpan();
*
* */
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView text1,text2,text3,text4;
private SpannableString spanString;
private ForegroundColorSpan span_ForegroundColorSpan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.edit);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
text4 = (TextView) findViewById(R.id.text4);
//改变字体颜色
//先构造SpannableString
spanString = new SpannableString("地地道道的欢迎光临Harvic的博客");
//1.再构造一个改变字体颜色(ForegroundColorSpan)
span_ForegroundColorSpan = new ForegroundColorSpan(Color.BLUE);
//2.改变字背景色(BackgroundColorSpan)
BackgroundColorSpan span_BackgroundColor = new BackgroundColorSpan(Color.YELLOW);
//3.改变字大小(AbsoluteSizeSpan)
AbsoluteSizeSpan span_AbsoluteSize = new AbsoluteSizeSpan(16);
//4.粗体、斜体(StyleSpan)
StyleSpan span_Style = new StyleSpan(Typeface.BOLD_ITALIC);
//5.删除线(StrikethroughSpan)
StrikethroughSpan span_Strikethrough = new StrikethroughSpan();
//6.下划线(UnderlineSpan)
UnderlineSpan span_Underline = new UnderlineSpan();
//7.图片置换(ImageSpan)
Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//也可以这样 ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic_launcher)
spanString.setSpan(span_ForegroundColorSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spanString.setSpan(span_BackgroundColor, 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spanString.setSpan(span_AbsoluteSize, 7, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spanString.setSpan(span, 1, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //SPAN_EXCLUSIVE_INCLUSIVE编辑框编辑的时候,后面的数字会跟着改变
// //设置给EditText显示出来
editText.setText(spanString);
init1();
setColor();
}
public void init1 () {
SpannableStringBuilder spannableString = new SpannableStringBuilder();
spannableString.append("暗影IV已经开始暴走了");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "请不要点我", Toast.LENGTH_SHORT).show();
}
};
spannableString.setSpan(clickableSpan, 5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
text1.setText(spannableString);
text1.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setColor () {
Spanned replyText = Html.fromHtml("交换机"+"<font color=" + getResources().getColor(R.color.colorAccent) + ">"+10548+"</font>");
String nae = getString(R.string.app_name);
text3.setText(Html.fromHtml(getString(R.string.draw_money,5555.368749,"222")));
text2.setText(replyText);
}
}