Android开发UI之textview实现高亮显示并点击跳转

textview实现高亮显示,带下划线,带背景,主要是通过SpannableString类实现。

具体实现请看代码:

1 TextView showMoreContent=(TextView)findviewbyid(R.id.showMoreContent);
2 SpannableString span=new SpannableString(showMoreContent.getText().toString()); 
3 span.setSpan(new ForegroundColorSpan(Color.RED), 0, showMoreContent.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//new ForegroundColorSpan()设置文字高亮显示
4 span.setSpan(new UnderlineSpan(), 0, showMoreContent.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//new UnderlineSpan()设置文字下划线
5 showMoreContent.setText(span);

设置背景色通过new BackgroundColorSpan(int)实现。

textview的跳转通过设置onClick

请看代码:

1 showMoreContent.setOnClickListener(new OnClickListener() {
2             
3             @Override
4             public void onClick(View v) {
5                 // TODO Auto-generated method stub
6                 Intent intent=new Intent(MainActivity.this,MoreContent.class);
7                 startActivity(intent);
8             }
9         });
原文地址:https://www.cnblogs.com/liyiran/p/4606220.html