Android TextView中文字设置超链接、颜色、字体 大杂烩
原文:http://blog.csdn.net/jy_sharer/article/details/12683771
TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象,可以借助Spannable对象实现以上设置。
效果图:  
 
Activity代码: 
- package com.zhou.activity;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableString;
- import android.text.Spanned;
- import android.text.method.LinkMovementMethod;
- import android.text.style.BackgroundColorSpan;
- import android.text.style.ForegroundColorSpan;
- import android.text.style.StyleSpan;
- import android.text.style.URLSpan;
- import android.widget.TextView;
- public class TextViewLinkActivity extends Activity {
- TextView myTextView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myTextView = (TextView) this.findViewById(R.id.myTextView);
- //创建一个 SpannableString对象
- SpannableString sp = new SpannableString("这句话中有百度超链接,有高亮显示,这样,或者这样,还有斜体.");
- //设置超链接
- sp.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置高亮样式一
- sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- //设置高亮样式二
- sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- //设置斜体
- sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- //SpannableString对象设置给TextView
- myTextView.setText(sp);
- //设置TextView可点击
- myTextView.setMovementMethod(LinkMovementMethod.getInstance());
- }
- }
Spannalbe文档:
Spannalbe This is the interface for text to which markup objects can be attached and detached. Not all Spannable classes have mutable text; see Editable for that.
setSpan方法:
public abstract void setSpan (Object what, int start, int end, int flags) Since: API Level 1 Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere. See Spanned for an explanation of what the flags mean. The object can be one that has meaning only within your application, or it can be one that the text system will use to affect text display or behavior. Some noteworthy ones are the subclasses of CharacterStyle and ParagraphStyle, and TextWatcher and SpanWatcher.
http://developer.android.com/referenc...
android 如何让文本中某个关键字高亮显示?
TextView tv = (TextView) findViewById(R.id.hello);
SpannableString s = new SpannableString(getResources().getString(R.string.linkify));
Pattern p = Pattern.compile("abc");
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(s);
//参数一:高亮颜色[ForegroundColorSpan前景色]
//from:高亮开始
//to:高亮结束
style.setSpan(new ForegroundColorSpan(color), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
(TextView)view.setText(style);
可以使用以下两种方法来实现:
1.用Html类的fromHtml()方法格式化要放到TextView里的文字。这种方法不仅能够高亮部分文字,而且还能够使用HTML里面方式来格式化文字,显示出各种效果。
上述代码把hello设置成红色。
2.使用Spannable或实现它的类,如SpannableString。Spannable对象也可以实现一样的效果
- SpannableString ss =newSpannableString("abcdefgh");
- ss.setSpan(newBackgroundColorSpan(Color.RED),2,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- TextView.setText(ss);
上述代码把[2,4)之间的字符设置成红色,也就是c和d。
1. 引言
在Android中,使某个字符串中的某个单词或汉字高亮,效果图及代码实现如下。
2. 效果图
     
3. 功能实现
1. 主界面(main.xml)实现:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:orientation = "vertical"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- >
- <TextView
- android:id = "@+id/highLight"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- />
- </LinearLayout>
2. 主Activity实现:
- package com.focus.fishme;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableStringBuilder;
- import android.text.style.BackgroundColorSpan;
- import android.widget.TextView;
- public class HighLightActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView highLightView = (TextView) findViewById(R.id.highLight);
- String highLightStr = "HighLight MaYingCai";
- String highLight = "MaYingCai";
- int start = highLightStr.indexOf(highLight);
- SpannableStringBuilder style = new SpannableStringBuilder(highLightStr);
- style.setSpan(new BackgroundColorSpan(Color.RED), start, start + highLight.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- highLightView.setText(style);
- }
- }
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号