标签:
CharacterStyle可以改变textview的一些属性,比如:背景色,前景色,下划线,点击事件等。效果如图:
点击button后,改变textview的一些属性的效果:
点击medium的时候弹出toast:
代码实例:
1 public class MainActivity extends Activity { 2 3 Button mbutton; 4 TextView mtextView; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 mtextView = (TextView) findViewById(R.id.mtextView); 12 mbutton = (Button) findViewById(R.id.mbutton); 13 14 mbutton.setOnClickListener(new OnClickListener() { 15 16 @Override 17 public void onClick(View v) { 18 19 SpannableStringBuilder ssBuilder = new SpannableStringBuilder( 20 mtextView.getText().toString()); 21 CharacterStyle cStyle = new ForegroundColorSpan(Color.BLUE); 22 CharacterStyle cStyle1 = new BackgroundColorSpan(Color.GRAY); 23 CharacterStyle cStyle2 = new UnderlineSpan(); 24 CharacterStyle cStyle3 = new ClickableSpan() { 25 26 @Override 27 public void onClick(View widget) { 28 Toast.makeText(getApplicationContext(), "hello", 29 Toast.LENGTH_LONG).show(); 30 31 } 32 }; 33 34 ssBuilder.setSpan(cStyle, 0, mtextView.getText().length(), 35 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 36 ssBuilder.setSpan(cStyle1, 0, mtextView.getText().length(), 37 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 38 ssBuilder.setSpan(cStyle2, 0, mtextView.getText().length(), 39 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 40 ssBuilder.setSpan(cStyle3, 0, mtextView.getText().length() - 5, 41 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 42 43 mtextView.setText(ssBuilder); 44 mtextView.setMovementMethod(LinkMovementMethod.getInstance()); 45 } 46 }); 47 48 } 49 50 }
标签:
原文地址:http://www.cnblogs.com/silenceshining/p/4662042.html