@Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_UP) return super.onTouchEvent(widget, buffer, event); 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); URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); if (link.length != 0) { onLinkClick(link[0].getURL()); } return true; }
private boolean shouldEnableDescription(TextView mDescription) { Layout layout = mDescription.getLayout(); if (layout != null && layout.getEllipsisCount(mDescription.getLineCount() - 1) > 0) { return true; } return false; }
@Override public void handleMessage(Message msg) { switch (msg.what) { case BluetoothConnection.MESSAGE_READ: byte[] readBuf = (byte[]) msg.obj; // construct a string from the valid bytes in the buffer String readMessage = new String(readBuf, 0, msg.arg1); // mConversationArrayAdapter.add(mConnectedDeviceName+": " + // readMessage); // debug(readMessage); // Indicate scanning in the title TextView displayedTextBox = (TextView) findViewById(R.id.recieved_text); displayedTextBox.append(readMessage); final int scrollAmount = displayedTextBox.getLayout().getLineTop(displayedTextBox.getLineCount()) - displayedTextBox.getHeight(); // if there is no need to scroll, scrollAmount will be <=0 if (scrollAmount > 0) { displayedTextBox.scrollTo(0, scrollAmount); } // else { // displayedTextBox.scrollTo(0,0); // } } }
private static int getLineAtCoordinate(float y, TextView tv, Layout layout) { if (tv.getLayout() == null) { y -= tv.getCompoundPaddingTop(); } else { y -= tv.getTotalPaddingTop(); } // Clamp the position to inside of the view. y = Math.max(0.0f, y); float bottom = tv.getHeight() - 1; if (tv.getLayout() == null) { bottom -= tv.getCompoundPaddingBottom(); } else { bottom -= tv.getTotalPaddingBottom(); } y = Math.min(bottom, y); y += tv.getScrollY(); return layout.getLineForVertical((int) y); }
private static float convertToLocalHorizontalCoordinate(float x, TextView tv) { if (tv.getLayout() == null) { x -= tv.getCompoundPaddingLeft(); } else { x -= tv.getTotalPaddingLeft(); } // Clamp the position to inside of the view. x = Math.max(0.0f, x); float rightSide = tv.getWidth() - 1; if (tv.getLayout() == null) { rightSide -= tv.getCompoundPaddingRight(); } else { rightSide -= tv.getTotalPaddingRight(); } x = Math.min(rightSide, x); x += tv.getScrollX(); return x; }
@Override public boolean onTouchEvent(MotionEvent event) { CharSequence text = getText(); int action = event.getAction(); if (!(text instanceof Spannable)) { return super.onTouchEvent(event); } Spannable buffer = (Spannable) text; if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { TextView widget = this; 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); URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { if (mCurrentLink == link[0]) { link[0].onClick(widget); } mCurrentLink = null; buffer.removeSpan(mLinkFocusStyle); } else if (action == MotionEvent.ACTION_DOWN) { mCurrentLink = link[0]; buffer.setSpan( mLinkFocusStyle, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return true; } } mCurrentLink = null; buffer.removeSpan(mLinkFocusStyle); return super.onTouchEvent(event); }
public boolean onTouchEvent(TextView widget, final Spannable buffer, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) position = event.getY(); // used to see if the user scrolled or not if (!(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN)) { if (Math.abs((position - event.getY())) > 10) { handler.removeCallbacksAndMessages(null); Log.v(LogUtil.getTag(), "POSITION NOT CLICK IS " + event.getY()); } return super.onTouchEvent(widget, buffer, event); } Log.v(LogUtil.getTag(), "POSITION IS " + position); comm = (SpoilerRobotoTextView) widget; 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); final int off = layout.getOffsetForHorizontal(line, x); link = buffer.getSpans(off, off, URLSpan.class); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: clickHandled = false; if (link.length != 0) { handler.postDelayed(longClicked, android.view.ViewConfiguration.getLongPressTimeout()); } break; case MotionEvent.ACTION_UP: handler.removeCallbacksAndMessages(null); if (!clickHandled) { // regular click if (link.length != 0) { int i = 0; if (sequence != null) { i = ((Spannable) sequence).getSpanEnd(link[0]); } onLinkClick(link[0].getURL(), i); } } break; } return true; }
private boolean right(TextView widget, Spannable buffer) { if (DBG) { Log.d(LOG_TAG, "--- right:"); } Layout layout = widget.getLayout(); int to = layout.getOffsetToRightOf(getEndPos(widget)); mManager.setSelectedEndPos(to); mManager.onCursorMoved(); return true; }
private Integer findOffsetForPosition(float x, float y) { if (childView == null || childView.getLayout() == null) { return null; } Layout layout = this.childView.getLayout(); int line = layout.getLineForVertical((int) y); return layout.getOffsetForHorizontal(line, x); }
/** * Helper method to pretty print object in the result_text field * * @param object */ private void println(Object object) { if (resultText == null) return; StringBuffer sb = new StringBuffer(resultText.getText()); String text; if (object == null) { text = "null"; } else { text = object.toString(); } sb.append(text).append("\n"); resultText.setText(sb); // Auto scroll to bottom if needed if (resultText.getLayout() != null) { int scroll = resultText.getLayout().getLineTop(resultText.getLineCount()) - resultText.getHeight(); resultText.scrollTo(0, scroll > 0 ? scroll : 0); } }
private void writeLog(String s) { TextView loginfo = (TextView) findViewById(R.id.info_area); loginfo.append(s + "\n"); // find the amount we need to scroll. This works by // asking the TextView's internal layout for the position // of the final line and then subtracting the TextView's height final int scrollAmount = loginfo.getLayout().getLineTop(loginfo.getLineCount()) - loginfo.getHeight(); // if there is no need to scroll, scrollAmount will be <=0 if (scrollAmount > 0) loginfo.scrollTo(0, scrollAmount); else loginfo.scrollTo(0, 0); }
/** 覆寫觸控事件,分辨出是否為連結的點擊。 */ @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 (widget instanceof LinkTextView) { // 如果實體是LinkTextView ((LinkTextView) widget).linkHit = true; } if (action == MotionEvent.ACTION_UP) { // 放開時 // link[0].onClick(widget); // 開啟連結 String url = ((URLSpan) link[0]).getURL(); Utils.clickUrl(widget, url); } else if (action == MotionEvent.ACTION_DOWN) { // 按下時 Selection.setSelection( buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); // 選擇連結 } return true; } else { // 不是連結點擊 if (widget instanceof LinkTextView) { // 如果實體是LinkTextView ((LinkTextView) widget).linkHit = false; } Selection.removeSelection(buffer); Touch.onTouchEvent(widget, buffer, event); return false; } } return Touch.onTouchEvent(widget, buffer, event); }
private boolean down(TextView widget, Spannable buffer) { if (DBG) { Log.d(LOG_TAG, "--- down:"); } Layout layout = widget.getLayout(); int end = getEndPos(widget); int line = layout.getLineForOffset(end); if (line < layout.getLineCount() - 1) { int to; if (layout.getParagraphDirection(line) == layout.getParagraphDirection(line + 1)) { float h = layout.getPrimaryHorizontal(end); to = layout.getOffsetForHorizontal(line + 1, h); } else { to = layout.getLineStart(line + 1); } mManager.setSelectedEndPos(to); mManager.onCursorMoved(); return true; } return false; }
@Override public boolean onTouchEvent(MotionEvent event) { TextView widget = (TextView) this; Object text = widget.getText(); if (text instanceof Spanned) { Spannable buffer = (Spannable) text; 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 false; }
@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); } commentView.invalidate(); return true; } else { // Changed this to propagate events PostView.this.onTouchEvent(event); return true; } } else { PostView.this.onTouchEvent(event); return true; } }
private 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; }
public void statusUpdate(String msg) { TextView tv = (TextView) MyApp.context.findViewById(R.id.textview); tv.append(msg); try { int lineTop = 0; try { lineTop = tv.getLayout().getLineTop(tv.getLineCount()); } catch (Throwable e) { lineTop = 0; } final int scrollAmount = lineTop - tv.getHeight(); if (scrollAmount > 0) { tv.scrollTo(0, scrollAmount); } else { tv.scrollTo(0, 0); } } catch (Throwable e) { Log.i(MyApp.TAG, e.getMessage().toString()); } }
@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); }
@Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_CANCEL) { 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) { // 按下后松开,移除所有 BackgroundColorSpan。 BackgroundColorSpan[] backgroundColorSpans = buffer.getSpans(0, buffer.length(), BackgroundColorSpan.class); for (BackgroundColorSpan bkcolor : backgroundColorSpans) { buffer.removeSpan(bkcolor); } // Selection.removeSelection(buffer); link[0].onClick(widget); } else if (action == MotionEvent.ACTION_DOWN) { // 按下,给按下的 ClickableSpan 设置 BackgroundColorSpan。 /* Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0])); */ BackgroundColorSpan bkcolor = new BackgroundColorSpan(0xff89660f); buffer.setSpan( bkcolor, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), Spanned.SPAN_INCLUSIVE_INCLUSIVE); } else if (action == MotionEvent.ACTION_CANCEL) { // 按下不松开而是移动,则变成取消事件,移除所有 BackgroundColorSpan。 BackgroundColorSpan[] backgroundColorSpans = buffer.getSpans(0, buffer.length(), BackgroundColorSpan.class); for (BackgroundColorSpan bkcolor : backgroundColorSpans) { buffer.removeSpan(bkcolor); } } return true; } else { BackgroundColorSpan[] backgroundColorSpans = buffer.getSpans(0, buffer.length(), BackgroundColorSpan.class); for (BackgroundColorSpan bkcolor : backgroundColorSpans) { buffer.removeSpan(bkcolor); } // Selection.removeSelection(buffer); } } return super.onTouchEvent(widget, buffer, event); }