对于文字的处理,普通的TextView和EditView都只是一个载体,Android提供了SpannableString,来对文字进行特殊美化,今天就带大家掌握这种方法吧。代码不多,仔细看哦!
package com.example.demo_tabwidget;
/**
* @author Arthur Lee
* @time 07/13/2014
* */
import org.xml.sax.SAXException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.URLSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button1, button2, button3, button4, button5, button6;
EditText tv;
SpannableString sp1 = new SpannableString("这段话中有网络的链接!");
SpannableString sp2 = new SpannableString("这段话中有网络的链接!");
SpannableString sp3 = new SpannableString("这段话中有网络的链接!");
SpannableString sp4 = new SpannableString("这段话中有网络的链接!");
SpannableString sp5 = new SpannableString("这段话中有网络的链接!");
SpannableString sp6 = new SpannableString("这段话中有网络的链接!");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (EditText)findViewById(R.id.tv);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button5 = (Button)findViewById(R.id.button5);
button6 = (Button)findViewById(R.id.button6);
button1.setOnClickListener(new MyListener());
button2.setOnClickListener(new MyListener());
button3.setOnClickListener(new MyListener());
button4.setOnClickListener(new MyListener());
button5.setOnClickListener(new MyListener());
button6.setOnClickListener(new MyListener());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//创建一个 SpannableString对象
/**
* Spannable的样式通过setSpan()方法设定,它有四个参数,分别表示为
* @param Object what 当前文字样式
* @param int start 样式起始位置
* @param int end 样式结束位置
* @param int flags 样式作用效果
* 其中,常用的效果有四种,分别如下:*/
/**
* Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)
* Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)
* Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)
* Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。ps:该效果只有在添加编辑时才能看到,所以本DEMO使用了EditView
* */
//设置高亮样式
sp1.setSpan(new BackgroundColorSpan(Color.RED), 5 ,7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sp2.setSpan(new BackgroundColorSpan(Color.RED), 5 ,7,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
sp3.setSpan(new BackgroundColorSpan(Color.RED), 5 ,7,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
sp4.setSpan(new BackgroundColorSpan(Color.RED), 5 ,7,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置超链接
sp5.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置斜体
sp6.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置EditText可点击
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyListener implements OnClickListener{
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.button1:
//SpannableString对象设置给TextView
tv.setText(sp1);
break;
case R.id.button2:
tv.setText(sp2);
break;
case R.id.button3:
tv.setText(sp3);
break;
case R.id.button4:
tv.setText(sp4);
break;
case R.id.button5:
tv.setText(sp5);
break;
case R.id.button6:
tv.setText(sp6);
break;
default:
break;
}
}
}
}
下面是效果图: