学徒浅析Android开发:第九讲——SpannableString,让文字活起来

               对于文字的处理,普通的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;
			}
		}
		
	}


}



      

下面是效果图:


             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值