码迷,mamicode.com
首页 > 移动开发 > 详细

[android篇]textview中片段响应点击事件(SpannableString)

时间:2014-06-14 12:15:35      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:android

  • 项目需求

点击textView中的一小段文字,弹一个dialog框


  • 失败解决方案

刚开始是用了两个textView水平布局,可想而知,当第一个textView快占满一行,还未换行时,第二个textView很可能出现换行排版问题


  • 用spannableString的问题

小段文字有下划线

点击textView中的小段文字时,系统会当做url处理,给点击部分的text加一个蓝色的背景


  • 解决方案

public class TouchableSpan extends ClickableSpan {
	    private boolean mIsPressed;
	    private int mPressedBackgroundColor;
	    private int mNormalTextColor;
	    private int mPressedTextColor;

	    public TouchableSpan(int normalTextColor, int pressedTextColor, int pressedBackgroundColor) {
	        mNormalTextColor = normalTextColor;
	        mPressedTextColor = pressedTextColor;
	        mPressedBackgroundColor = pressedBackgroundColor;
	    }

	    public void setPressed(boolean isSelected) {
	        mIsPressed = isSelected;
	    }

	    @Override
	    public void updateDrawState(TextPaint ds) {
	        super.updateDrawState(ds);
	        ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);
	        ds.bgColor = mIsPressed ? mPressedBackgroundColor : 0x00eeeeee;
	        ds.setUnderlineText(false);
	    }

		@Override
		public void onClick(View widget) {
			//todo
		}
	}
class LinkTouchMovementMethod extends LinkMovementMethod {
	    private TouchableSpan 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) {
	                mPressedSpan.setPressed(true);
	                Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan),
	                        spannable.getSpanEnd(mPressedSpan));
	            }
	        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
	            TouchableSpan touchedSpan = getPressedSpan(textView, spannable, event);
	            if (mPressedSpan != null && touchedSpan != mPressedSpan) {
	                mPressedSpan.setPressed(false);
	                mPressedSpan = null;
	                Selection.removeSelection(spannable);
	            }
	        } else {
	            if (mPressedSpan != null) {
	                mPressedSpan.setPressed(false);
	                super.onTouchEvent(textView, spannable, event);
	            }
	            mPressedSpan = null;
	            Selection.removeSelection(spannable);
	        }
	        return true;
	    }

	    TouchableSpan 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);

	        TouchableSpan[] link = spannable.getSpans(off, off, TouchableSpan.class);
	        TouchableSpan touchedSpan = null;
	        if (link.length > 0) {
	            touchedSpan = link[0];
	        }
	        return touchedSpan;
	    }

	}

最后别忘了加上

mStartPageTermsCondition.setMovementMethod(new LinkTouchMovementMethod());



[android篇]textview中片段响应点击事件(SpannableString),布布扣,bubuko.com

[android篇]textview中片段响应点击事件(SpannableString)

标签:android

原文地址:http://blog.csdn.net/vic_fang/article/details/30494527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!