android.text.method.Touch Java Examples

The following examples show how to use android.text.method.Touch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalLinkMovementMethod.java    From JianshuApp with GNU General Public License v3.0 5 votes vote down vote up
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();
    if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_DOWN) {
        return Touch.onTouchEvent(widget, buffer, event);
    }

    int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
    int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
    Layout layout = widget.getLayout();
    int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
    ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
    if (link.length != 0) {
        if (action == MotionEvent.ACTION_UP) {
            link[0].onClick(widget);
        } else {
            Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
        }
        if (!(widget instanceof TextViewFixTouchConsume)) {
            return true;
        }
        ((TextViewFixTouchConsume) widget).linkHit = true;
        return true;
    }
    Selection.removeSelection(buffer);
    Touch.onTouchEvent(widget, buffer, event);
    return false;
}
 
Example #2
Source File: NameTouchMovementMethod.java    From meiShi with Apache License 2.0 5 votes vote down vote up
public boolean onTouchEvent(TextView widget, Spannable buffer,
                            MotionEvent event) {
    int offSet = getOffsetForHorizontal(widget, event);
    ClickableSpan[] spanName = buffer.getSpans(offSet, offSet, ClickableSpan.class);
    if (spanName.length == 0) {
        Selection.removeSelection(buffer);
        return Touch.onTouchEvent(widget, buffer, event);
    }
    ClickableSpan span = spanName[0];
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            setSapnBackgroundGrayEffect(span, buffer);
            Selection.setSelection(buffer,
                    buffer.getSpanStart(span),
                    buffer.getSpanEnd(span));
            break;
        case MotionEvent.ACTION_UP:
            newClickTask(new ClickTask(span, widget));
            resetSapnBackground(span, buffer);
            break;
        case MotionEvent.ACTION_CANCEL:
            removeAllTask();
            resetSapnBackground(span, buffer);
            break;
    }
    return Touch.onTouchEvent(widget, buffer, event);
}
 
Example #3
Source File: CustomMovementMethod.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
                            MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP ||
            action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }
            return true;
        }
    }

    return Touch.onTouchEvent(widget, buffer, event);
}
 
Example #4
Source File: LocalLinkMovementMethod.java    From html-textview with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP ||
            action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            return true;
        } else {
            Selection.removeSelection(buffer);
            Touch.onTouchEvent(widget, buffer, event);
            return false;
        }
    }
    return Touch.onTouchEvent(widget, buffer, event);
}
 
Example #5
Source File: TextViewFixTouchConsume.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
		MotionEvent event) {
	int action = event.getAction();

	if (action == MotionEvent.ACTION_UP
			|| action == MotionEvent.ACTION_DOWN) {
		int x = (int) event.getX();
		int y = (int) event.getY();

		x -= widget.getTotalPaddingLeft();
		y -= widget.getTotalPaddingTop();

		x += widget.getScrollX();
		y += widget.getScrollY();

		Layout layout = widget.getLayout();
		int line = layout.getLineForVertical(y);
		int off = layout.getOffsetForHorizontal(line, x);

		ClickableSpan[] link = buffer.getSpans(off, off,
				ClickableSpan.class);

		if (link.length != 0) {
			if (action == MotionEvent.ACTION_UP) {
				link[0].onClick(widget);
			} else if (action == MotionEvent.ACTION_DOWN) {
				Selection.setSelection(buffer,
						buffer.getSpanStart(link[0]),
						buffer.getSpanEnd(link[0]));
			}

			if (widget instanceof TextViewFixTouchConsume) {
				((TextViewFixTouchConsume) widget).linkHit = true;
			}
			return true;
		} else {
			Selection.removeSelection(buffer);
			Touch.onTouchEvent(widget, buffer, event);
			return false;
		}
	}
	return Touch.onTouchEvent(widget, buffer, event);
}
 
Example #6
Source File: WeiboTextView.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
@Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            int action = event.getAction();

//            LogUtils.printLog(WeiboTextView.class, "MotionEvent:" + action);

//            if (action == MotionEvent.ACTION_MOVE) {
//                isPressed = false;
//            }
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_UP:
                    break;

                case MotionEvent.ACTION_CANCEL:
                    buffer.setSpan(new BackgroundColorSpan(0x00000000), 0, weiboTextView.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    Selection.setSelection(buffer, 0, weiboTextView.length());
                    break;
            }


            if (action == MotionEvent.ACTION_UP ||
                    action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();

                x += widget.getScrollX();
                y += widget.getScrollY();

                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

                if (link.length != 0) {

                    int start = buffer.getSpanStart(link[0]);
                    int end = buffer.getSpanEnd(link[0]);

//                    LogUtils.printLog(WebViewUtils.class, "start:" + start + " end:" + end);

                    if (action == MotionEvent.ACTION_UP) {
                        link[0].onClick(widget);
                        buffer.setSpan(new BackgroundColorSpan(0x00000000), start, end,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        Selection.setSelection(buffer, start, end);
                    } else if (action == MotionEvent.ACTION_DOWN) {
                        buffer.setSpan(new BackgroundColorSpan(weiboTextView.clickBgClolr), start, end,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        Selection.setSelection(buffer, start, end);
                    }

                    if (widget instanceof WeiboTextView) {
                        ((WeiboTextView) widget).linkHit = true;
                    }
                    return true;
                } else {
                    Selection.removeSelection(buffer);
                    Touch.onTouchEvent(widget, buffer, event);
                    return false;
                }
            }


            return Touch.onTouchEvent(widget, buffer, event);
        }
 
Example #7
Source File: TextViewFixTouchConsume.java    From umeng_community_android with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP ||
            action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();

        x += widget.getScrollX();
        y += widget.getScrollY();

        Layout layout = widget.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                link[0].onClick(widget);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer,
                        buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0]));
            }

            if (widget instanceof TextViewFixTouchConsume) {
                ((TextViewFixTouchConsume) widget).mlinkHit = true;
            }
            return true;
        } else {
            Selection.removeSelection(buffer);
            Touch.onTouchEvent(widget, buffer, event);
            return false;
        }
    }
    return Touch.onTouchEvent(widget, buffer, event);
}
 
Example #8
Source File: MyMovementMethod.java    From TLint with Apache License 2.0 4 votes vote down vote up
@Override
 public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
     if (mGray == null) {
         mGray = new BackgroundColorSpan(Color.parseColor("#eaeaea"));
     }

     mIsLinkHit = false;

     int action = event.getAction();

     if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
         int x = (int) event.getX();
         int y = (int) event.getY();

/*if (DEBUG) {
             Log.d(TAG, "x = " + x + " y = " + y);
}*/

         x -= widget.getTotalPaddingLeft();
         y -= widget.getTotalPaddingTop();

/*if (DEBUG) {
             Log.d(TAG, "x = " + x + " y = " + y);
}*/

         x += widget.getScrollX();
         y += widget.getScrollY();

         int line = widget.getLayout().getLineForVertical(y);
         int offset = widget.getLayout().getOffsetForHorizontal(line, x);

         ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);

/*if (DEBUG) {
             Log.d(TAG, "x = " + x + " y = " + y);
	Log.d(TAG, "line = " + line + " offset = " + offset);
	Log.d(TAG, "spans.lenth = " + spans.length);
}*/

         if (spans.length != 0) {
             int start = buffer.getSpanStart(spans[0]);
             int end = buffer.getSpanEnd(spans[0]);

             mIsLinkHit = true;

             if (action == MotionEvent.ACTION_DOWN) {
                 /*if (DEBUG) {
                     Log.d(TAG, "Down event detected");
		}*/

                 buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             } else if (action == MotionEvent.ACTION_UP) {
       /*if (DEBUG) {
         Log.d(TAG, "Up event detected");
		}*/

                 spans[0].onClick(widget);

                 buffer.removeSpan(mGray);
             }

             return true;
         }
     } else {
         buffer.removeSpan(mGray);
     }

     return Touch.onTouchEvent(widget, buffer, event);
 }
 
Example #9
Source File: HackyMovementMethod.java    From BlackLight with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
	if (mGray == null) {
		mGray = new BackgroundColorSpan(widget.getContext().getResources().getColor(R.color.selector_gray));
	}
	
	mIsLinkHit = false;
	
	int action = event.getAction();
	
	if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		
		if (DEBUG) {
			Log.d(TAG, "x = " + x + " y = " + y);
		}
		
		x -= widget.getTotalPaddingLeft();
		y -= widget.getTotalPaddingTop();
		
		if (DEBUG) {
			Log.d(TAG, "x = " + x + " y = " + y);
		}
		
		x += widget.getScrollX();
		y += widget.getScrollY();
		
		int line = widget.getLayout().getLineForVertical(y);
		int offset = widget.getLayout().getOffsetForHorizontal(line, x);
		
		ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);
		
		if (DEBUG) {
			Log.d(TAG, "x = " + x + " y = " + y);
			Log.d(TAG, "line = " + line + " offset = " + offset);
			Log.d(TAG, "spans.lenth = " + spans.length);
		}
		
		if (spans.length != 0) {
			int start = buffer.getSpanStart(spans[0]);
			int end = buffer.getSpanEnd(spans[0]);
			
			mIsLinkHit = true;
			
			if (action == MotionEvent.ACTION_DOWN) {
				if (DEBUG) {
					Log.d(TAG, "Down event detected");
				}
				
				buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			} else if (action == MotionEvent.ACTION_UP) {
				if (DEBUG) {
					Log.d(TAG, "Up event detected");
				}
				
				spans[0].onClick(widget);
				
				buffer.removeSpan(mGray);
			}
			
			return true;
		}
	} else {
		buffer.removeSpan(mGray);
	}
	
	return Touch.onTouchEvent(widget, buffer, event);
}