实现效果 :思路主要是使用TextView和SpanableStringBuilder的setSpan方法实现的
参考文档http://androiddoc.qiniudn.com/reference/android/text/SpannableStringBuilder.html#setSpan(java.lang.Object, int, int, int)
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="themeone.testtablayout.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:id="@+id/tv_recieve" android:layout_height="wrap_content" android:text=""/> </RelativeLayout>
主要代码思路
public class MainActivity extends Activity implements View.OnClickListener { private TextView tv_recieve; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_recieve = (TextView) findViewById(R.id.tv_recieve); tv_recieve.setOnClickListener(this); //仿qq空间点赞text文本分段点击事件 setTextMultClick(); } //准备数据 private void setTextMultClick() { StringBuilder sb=new StringBuilder(); for (int i = 0; i < 20; i++) { sb.append("好友"+i+","); } String users=sb.substring(0,sb.lastIndexOf(",")); tv_recieve.setText(addClickPart(users), TextView.BufferType.SPANNABLE); } //定义点击每个部分文字的处理方法 private SpannableStringBuilder addClickPart(String users) { ImageSpan imageSpan=new ImageSpan(getApplicationContext(),R.mipmap.ic_launcher); SpannableString spanStr=new SpannableString("i.");//任意文字 主要是实现效果 //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 前后都不包括 spanStr.setSpan(imageSpan,0,1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //创建一个ssb 存储总的用户 SpannableStringBuilder ssb=new SpannableStringBuilder(spanStr); ssb.append(users); //为每段数据创建点击 事件 String[] users_array=users.split(","); if(users_array.length>0){ for (int i = 0; i < users_array.length; i++) { final String user_name = users_array[i];//好友0 int start=users.indexOf(user_name)+spanStr.length(); //为每段数据增加点击事件 ssb.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this,user_name, Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(Color.BLUE); // ds.setUnderlineText(false); } },start,start+user_name.length(),0); } } return ssb.append("等"+users_array.length+"人觉得很赞"); }
}