package com.example.peng.butterknifetest;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.method.Touch;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Peng on 2018/1/13.
*/
@SuppressLint("AppCompatCustomView")
public class UICommentTextView extends TextView {
private final static int REPLY = 0x1;
private final static int REPLY_TO = 0x2;
private static boolean mIsInteriorClick = false;//内部是否被点击
private static boolean mIsLongClick = false;//是否是长按
private static final int COMMENTATOR_USER_HINT_COLOR = 0xFF0000FF;
private static final int COMMENTATOR_HINT_BG_PRESSED = 0xFFE0E0E0;
private static final int COMMENTATOR_HINT_BG_NORMAL = 0x00000000;
private static final int UI_BACKGROUND_RESOURCE_PRESSED_COLOR = 0xFFE0E0E0;
private static final int UI_BACKGROUND_RESOURCE_NORMAL_COLOR = 0x00000000;
private static int mUserHintColor = COMMENTATOR_USER_HINT_COLOR;
private static int mUserHintBgPressedColor = COMMENTATOR_HINT_BG_PRESSED;
private static int mUserHintBgNormalColor = COMMENTATOR_HINT_BG_NORMAL;
private static int mViewBackgroundPressedColor = UI_BACKGROUND_RESOURCE_PRESSED_COLOR;
private static int mViewBackgroundNormalColor = UI_BACKGROUND_RESOURCE_NORMAL_COLOR;
private static boolean mIsLongClickCopyOf = false;
private String REPLY_HINT = "回复";
private CommentTextViewListener commentTextViewListener;
public UICommentTextView(Context context) {
super(context);
}
public UICommentTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttr(context,attrs);
}
public UICommentTextView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttr(context,attrs);
}
private void initAttr(Context context, AttributeSet attrs) {
if (attrs == null) return;
TypedArray type = getContext().obtainStyledAttributes(attrs, R.styleable.UICommentTextView);
if (type == null) return;
for (int i = 0; i < type.getIndexCount(); i++) {
int attr = type.getIndex(i);
if (attr == R.styleable.UICommentTextView_comment_UserHintColor){
mUserHintColor = type.getInteger(attr,COMMENTATOR_USER_HINT_COLOR);
}else if (attr == R.styleable.UICommentTextView_comment_UserHintBgPressedColor){
mUserHintBgPressedColor = type.getInteger(attr,COMMENTATOR_HINT_BG_PRESSED);
}else if (attr == R.styleable.UICommentTextView_comment_UserHintBgNormalColor){
mUserHintBgNormalColor = type.getInteger(attr,COMMENTATOR_HINT_BG_NORMAL);
}else if (attr == R.styleable.UICommentTextView_comment_ViewBackgroundPressedColor){
mViewBackgroundPressedColor = type.getInteger(attr,UI_BACKGROUND_RESOURCE_PRESSED_COLOR);
}else if (attr == R.styleable.UICommentTextView_comment_ViewBackgroundNormalColor){
mViewBackgroundNormalColor = type.getInteger(attr,UI_BACKGROUND_RESOURCE_NORMAL_COLOR);
}else if (attr == R.styleable.UICommentTextView_comment_IsLongClickCopyOf){
mIsLongClickCopyOf = type.getBoolean(attr,false);
}
}
type.recycle();
}
/**
* 设置显示数据
*
* @param commentator 回复人
* @param content 回复内容
*/
public void setTextContent(String commentator,String content){
setTextContent(commentator,null ,content);
}
/**
* 设置显示数据
*
* @param commentator 回复人
* @param criticised 被回复人
* @param content 回复内容
*/
public void setTextContent(String commentator,String criticised,String content){
SpannableStringBuilder ssb = new SpannableStringBuilder();
setMovementMethod(LinkTouchMovementMethod.getInstance());
if (commentator != null){
ssb.append(commentator);
ssb.setSpan(new TouchableSpan(REPLY),0,commentator.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (criticised != null){
ssb.append(REPLY_HINT);
ssb.append(criticised);
ssb.setSpan(new TouchableSpan(REPLY_TO),
commentator.length()+REPLY_HINT.length(),
commentator.length()+REPLY_HINT.length()+criticised.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (content != null){
ssb.append(":"+content);
}
this.setText(ssb,BufferType.NORMAL);
}
private class TouchableSpan extends ClickableSpan{
private boolean mIsPressed = false;
private int clickType;
public TouchableSpan(int clickType) {
this.clickType = clickType;
}
public void setIsPressed(boolean mIsPressed) {
this.mIsPressed = mIsPressed;
}
@Override
public void onClick(View widget) {
switch (clickType){
case REPLY:
if (commentTextViewListener != null) {
commentTextViewListener.OnReplyClick(getContext());
}
break;
case REPLY_TO:
if (commentTextViewListener != null) {
commentTextViewListener.OnReplyToClick(getContext());
}
break;
}
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(mUserHintColor);
ds.bgColor = mIsPressed ? mUserHintBgPressedColor : mUserHintBgNormalColor;
ds.setUnderlineText(false);
}
}
/**
* 自定义LinkMovementMethod
*/
public static class LinkTouchMovementMethod extends LinkMovementMethod{
static LinkTouchMovementMethod sInstance;
private TouchableSpan mTouchableSpan;
public static LinkTouchMovementMethod getInstance(){
if (sInstance == null){
sInstance = new LinkTouchMovementMethod();
}
return sInstance;
}
@Override
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN){
mTouchableSpan = getPressedSpan(textView,spannable,event);
if (mTouchableSpan != null){
mIsLongClick = false;
mTouchableSpan.setIsPressed(true);
Selection.setSelection(spannable,spannable.getSpanStart(mTouchableSpan),
spannable.getSpanEnd(mTouchableSpan));
mIsInteriorClick = true;
}else {
textView.setBackgroundColor(mViewBackgroundPressedColor);
}
} else if (action == MotionEvent.ACTION_UP){
if (mTouchableSpan != null){
mTouchableSpan.setIsPressed(false);
if (
Android 高仿微信朋友评论列表与点击事件
需积分: 16 18 浏览量
2018-04-16
17:49:23
上传
评论 1
收藏 141KB ZIP 举报
WL-鬼
- 粉丝: 32
- 资源: 13
最新资源
- flutter appflutter app
- 扫描全能王 2024-05-10 16.45.pdf
- spire.office.free-5.3.1.jar
- 数字电路:异步触发器计数器分频器 Quartus II 环境Verilog HDL语言编程+仿真+开发板FPGACPLDEDA
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
- CSDN_1715329934159.png
- Node安装教程(附安装包)
- 对点云更改重心位置和尺度python代码
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
- 实验五朴素贝叶斯西瓜数据集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈