1. 引言

在Android 中,使某个字符串中的某个单词或汉字高亮,效果图及代码实现如下。

2. 效果图

3. 功能实现

1. 主界面(main.xml)实现:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:Android = "http://schemas.android.com/apk/res/android"
  3. Android :orientation= "vertical"
  4. Android :layout_width= "fill_parent"
  5. Android :layout_height= "fill_parent"
  6. >
  7. <TextView
  8. Android :id= "@+id/highLight"
  9. Android :layout_width= "wrap_content"
  10. Android :layout_height= "wrap_content"
  11. />
  12. </LinearLayout>

2. 主Activity实现:

  1. package com.focus.fishme;
  2. import Android .app.Activity;
  3. import Android .graphics.Color;
  4. import Android .os.Bundle;
  5. import Android .text.Spannable;
  6. import Android .text.SpannableStringBuilder;
  7. import Android .text.style.BackgroundColorSpan;
  8. import Android .widget.TextView;
  9. public class HighLightActivity extends Activity{
  10. @Override
  11. public void onCreate(BundlesavedInstanceState){
  12. super .onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. TextViewhighLightView=(TextView)findViewById(R.id.highLight);
  15. StringhighLightStr= "HighLightMaYingCai" ;
  16. StringhighLight= "MaYingCai" ;
  17. int start=highLightStr.indexOf(highLight);
  18. SpannableStringBuilderstyle= new SpannableStringBuilder(highLightStr);
  19. style.setSpan( new BackgroundColorSpan(Color.RED),start,start+highLight.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  20. /////////// style.setSpan(new ForegroundColorSpan(Color.RED),3,8,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  21. highLightView.setText(style);
  22. }
  23. }

更多相关文章

  1. Android(安卓)代码执行Linux Shell小记
  2. android 横向滚动屏幕实现(1)
  3. Android(安卓)开源项目分类汇总
  4. Android(安卓)实现跑马灯效果
  5. Android实现发送短信功能实例详解
  6. android全屏去掉title栏的多种实现方法
  7. Android中字符串片段高亮
  8. android中分割线的实现
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. android编译步骤
  2. Android中.9.png图片的使用过程和原理
  3. A06_RelativeLayout的属性设置
  4. 为Android内核添加hello world驱动并添加
  5. Qt5.8开发Android:强制横屏
  6. Android(安卓)各类生命周期详解
  7. 玩转FragmentTabHost,实现底部导航栏
  8. android:imeOptions 控制软键盘右下角按
  9. Android:CourseTableLayout — 好用的Andr
  10. Android(安卓)屏幕横竖屏切换