其实实现起来还是比较容易的,主要是把原理想清楚一切都好办。
1.主要使用SpannableString类,该类有setSpan(what, start, end, flags)方法,使用该方法便可以对textview设置想要的效果了,这里的what就是效果名,start和end是设置这个样式针对的是textview的字符位置。
2.主要代码CommentItemView.java
<pre name="code" class="plain">import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* 评论显示itemView
*
* @author admin
*
*/
public class CommentItemView extends TextView {
public CommentItemView(Context context) {
super(context);
initView();
}
public CommentItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView() {
setTextColor(getResources().getColor(R.color.message_text_gray));
setHighlightColor(getResources().getColor(R.color.color_e0e0e0));
setPadding(0, 5, 0, 5);
}
public boolean setClick() {
String from = "小明";
String to = "小李";
String content = "你好啊";
if (TextUtils.isEmpty(from))
return false;
final String fromusername = from;
String str = "";
boolean hasNext = false;
if (TextUtils.isEmpty(to)) {
str = from + ":" + content;
} else {
str = from + ":" + "回复:" + to + ":" + content;
hasNext = true;
}
SpannableString ss = new SpannableString(str);
int start = 0, end = from.length() + 1;
ss.setSpan(new NoLineCllikcSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(getContext(), "点击小明", Toast.LENGTH_SHORT).show();
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(
new ForegroundColorSpan(getResources().getColor(
R.color.color_91a9d0)), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (hasNext) {
start = end + 3;
end = start + to.length() + 1;
ss.setSpan(new NoLineCllikcSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(getContext(), "点击小李", Toast.LENGTH_SHORT)
.show();
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(
new ForegroundColorSpan(getResources().getColor(
R.color.color_91a9d0)), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
setText(ss);
setMovementMethod(new LinkTouchMovementMethod());
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "点击整个评论", Toast.LENGTH_SHORT)
.show();
}
});
return true;
}
public class NoLineCllikcSpan extends ClickableSpan {
public NoLineCllikcSpan() {
super();
}
@Override
public void updateDrawState(TextPaint ds) {
/** set textColor **/
ds.setColor(ds.linkColor);
/** Remove the underline **/
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
}
}
private class LinkTouchMovementMethod extends LinkMovementMethod {
private NoLineCllikcSpan mPressedSpan;
@Override
public boolean onTouchEvent(TextView textView, Spannable spannable,
MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mPressedSpan = getPressedSpan(textView, spannable, event);
if (mPressedSpan != null) {
setBackgroundResource(R.drawable.comment_item_spann_click_selector);
} else {
setBackgroundResource(R.drawable.comment_item_click_selector);
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (mPressedSpan != null) {
setBackgroundResource(R.drawable.comment_item_spann_click_selector);
}
mPressedSpan = null;
}
return super.onTouchEvent(textView, spannable, event);
}
private NoLineCllikcSpan getPressedSpan(TextView textView,
Spannable spannable, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
NoLineCllikcSpan[] link = spannable.getSpans(off, off,
NoLineCllikcSpan.class);
NoLineCllikcSpan touchedSpan = null;
if (link.length > 0) {
touchedSpan = link[0];
}
return touchedSpan;
}
}
}
用到的资源文件
<color name="message_text_gray">#757575</color>
<color name="color_e0e0e0">#e0e0e0</color>
<color name="color_91a9d0">#91a9d0</color>
R.drawable.comment_item_spann_click_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/transparent" android:state_pressed="true" />
<item android:drawable="@color/transparent" android:state_focused="true"/>
<item android:drawable="@color/transparent" android:state_selected="true"/>
<item android:drawable="@color/transparent"/>
</selector>
R.drawable.comment_item_click_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_e0e0e0" android:state_pressed="true" />
<item android:drawable="@color/color_e0e0e0" android:state_focused="true"/>
<item android:drawable="@color/color_e0e0e0" android:state_selected="true"/>
<item android:drawable="@color/transparent"/>
</selector>