Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [android篇]textview中片段響應點擊事件(SpannableString)

[android篇]textview中片段響應點擊事件(SpannableString)

編輯:關於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());



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved