對於文字的處理,普通的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;
}
}
}
}
下面是效果圖: