Search in sources :

Example 51 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by AOSPA.

the class TextView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    if (mEditor != null) {
        mEditor.onTouchEvent(event);
        if (mEditor.mSelectionModifierCursorController != null && mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
            return true;
        }
    }
    final boolean superResult = super.onTouchEvent(event);
    /*
         * Don't handle the release after a long press, because it will move the selection away from
         * whatever the menu action was trying to affect. If the long press should have triggered an
         * insertion action mode, we can now actually show it.
         */
    if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
        mEditor.mDiscardNextActionUp = false;
        if (mEditor.mIsInsertionActionModeStartPending) {
            mEditor.startInsertionActionMode();
            mEditor.mIsInsertionActionModeStartPending = false;
        }
        return superResult;
    }
    final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;
        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
        }
        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), getSelectionEnd(), ClickableSpan.class);
            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
                handled |= imm != null && imm.showSoftInput(this, 0);
            }
            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);
            handled = true;
        }
        if (handled) {
            return true;
        }
    }
    return superResult;
}
Also used : InputMethodManager(android.view.inputmethod.InputMethodManager) ClickableSpan(android.text.style.ClickableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Example 52 with ClickableSpan

use of android.text.style.ClickableSpan in project wire-android by wireapp.

the class TextViewUtils method linkifyText.

public static void linkifyText(TextView textView, final int highlightColor, int boldTypeface, final boolean underline, final Runnable onClick) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    final String string = textView.getText().toString();
    final int highlightStart = string.indexOf('_');
    if (highlightStart < 0) {
        Timber.e("Failed to highlight text - could not find _ marker in string.");
        return;
    }
    final int highlightEnd = string.lastIndexOf('_') - 1;
    if (highlightStart >= highlightEnd) {
        Timber.e("Failed to highlight text - make sure you have 2 _ markers to denote start and end of highlight region");
        return;
    }
    final SpannableStringBuilder str = new SpannableStringBuilder(textView.getText());
    str.replace(highlightStart, (highlightStart + 1), "");
    str.replace(highlightEnd, (highlightEnd + 1), "");
    final Typeface typeface = textView.getTypeface();
    ClickableSpan linkSpan = new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            if (onClick == null) {
                return;
            }
            onClick.run();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(underline);
            ds.setTypeface(typeface);
            ds.setColor(highlightColor);
        }
    };
    str.setSpan(linkSpan, highlightStart, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (boldTypeface > 0) {
        str.setSpan(new CustomTypefaceSpan("", textView.getResources().getString(boldTypeface)), highlightStart, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setText(str);
}
Also used : Typeface(android.graphics.Typeface) SpannableString(android.text.SpannableString) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SpannableStringBuilder(android.text.SpannableStringBuilder) TextPaint(android.text.TextPaint)

Example 53 with ClickableSpan

use of android.text.style.ClickableSpan in project RichText by zzhoujay.

the class CachedSpannedParser method handleImage.

private int handleImage(SpannableStringBuilder ssb, ImageGetterWrapper imageGetterWrapper, RichTextConfig config, boolean cached) {
    if (cached) {
        ClickableImageSpan[] cis = ssb.getSpans(0, ssb.length(), ClickableImageSpan.class);
        if (cis != null && cis.length > 0) {
            for (ClickableImageSpan ci : cis) {
                int start = ssb.getSpanStart(ci);
                int end = ssb.getSpanEnd(ci);
                ssb.removeSpan(ci);
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(ci.getSource());
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan nci = new ClickableImageSpan(drawable, ci, onImageClickListener, onImageLongClickListener);
                ssb.setSpan(nci, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return cis.length;
        }
    } else if (!config.noImage) {
        ImageSpan[] iss = ssb.getSpans(0, ssb.length(), ImageSpan.class);
        if (iss != null && iss.length > 0) {
            ArrayList<String> imageUrls = new ArrayList<>(iss.length);
            for (int i = 0; i < iss.length; i++) {
                ImageSpan imageSpan = iss[i];
                String imageUrl = imageSpan.getSource();
                imageUrls.add(imageUrl);
                int start = ssb.getSpanStart(imageSpan);
                int end = ssb.getSpanEnd(imageSpan);
                ClickableSpan[] clickableSpans = ssb.getSpans(start, end, ClickableSpan.class);
                if (clickableSpans != null && clickableSpans.length != 0) {
                    for (ClickableSpan cs : clickableSpans) {
                        ssb.removeSpan(cs);
                    }
                }
                OnImageClickListener onImageClickListener = null;
                OnImageLongClickListener onImageLongClickListener = null;
                if (config.clickable > 0) {
                    onImageClickListener = config.onImageClickListener;
                    onImageLongClickListener = config.onImageLongClickListener;
                }
                Drawable drawable = imageGetterWrapper.getDrawable(imageUrl);
                if (drawable == null) {
                    drawable = new ColorDrawable(Color.TRANSPARENT);
                }
                ClickableImageSpan cacheImageSpan = new ClickableImageSpan(drawable, imageUrls, i, onImageClickListener, onImageLongClickListener);
                ssb.removeSpan(imageSpan);
                ssb.setSpan(cacheImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return iss.length;
        }
    }
    return 0;
}
Also used : OnImageClickListener(com.zzhoujay.richtext.callback.OnImageClickListener) ColorDrawable(android.graphics.drawable.ColorDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) ArrayList(java.util.ArrayList) OnImageLongClickListener(com.zzhoujay.richtext.callback.OnImageLongClickListener) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ClickableSpan(android.text.style.ClickableSpan) ClickableImageSpan(com.zzhoujay.richtext.spans.ClickableImageSpan) ImageSpan(android.text.style.ImageSpan)

Example 54 with ClickableSpan

use of android.text.style.ClickableSpan in project AndroidUtilCode by Blankj.

the class SpannableActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spannable);
    ClickableSpan clickableSpan = new ClickableSpan() {

        @Override
        public void onClick(View widget) {
            ToastUtils.showShortToast("事件触发了");
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.BLUE);
            ds.setUnderlineText(false);
        }
    };
    TextView tvAboutSpannable = (TextView) findViewById(R.id.tv_about_spannable);
    // 响应点击事件的话必须设置以下属性
    tvAboutSpannable.setMovementMethod(LinkMovementMethod.getInstance());
    tvAboutSpannable.setText(SpannableStringUtils.getBuilder("测试SpannableStringUtils\n").setBold().setForegroundColor(Color.YELLOW).setBackgroundColor(Color.GRAY).setAlign(Layout.Alignment.ALIGN_CENTER).append("测试").append("前景色").setForegroundColor(Color.GREEN).append("背景色\n").setBackgroundColor(Color.RED).append("测试首行缩进\n").setLeadingMargin(30, 50).append("测试引用\n").setQuoteColor(Color.YELLOW).append("测试列表项\n").setBullet(30, Color.YELLOW).append("测试").append("2倍字体\n").setProportion(2).append("测试").append("横向2倍字体\n").setXProportion(2).append("测试").append("删除线").setStrikethrough().append("下划线\n").setUnderline().append("测试").append("上标").setSuperscript().append("下标\n").setSubscript().append("测试").append("粗体").setBold().append("斜体").setItalic().append("粗斜体\n").setBoldItalic().append("monospace font\n").setFontFamily("monospace").append("serif font\n").setFontFamily("serif").append("sans-serif font\n").setFontFamily("sans-serif").append("测试正常对齐\n").setAlign(Layout.Alignment.ALIGN_NORMAL).append("测试居中对齐\n").setAlign(Layout.Alignment.ALIGN_CENTER).append("测试相反对齐\n").setAlign(Layout.Alignment.ALIGN_OPPOSITE).append("测试").append("图片\n").setResourceId(R.mipmap.ic_launcher).append("测试").append("点击事件\n").setClickSpan(clickableSpan).append("测试").append("Url\n").setUrl("https://github.com/Blankj/AndroidUtilCode").append("测试").append("模糊字体\n").setBlur(3, BlurMaskFilter.Blur.NORMAL).create());
}
Also used : TextView(android.widget.TextView) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) TextPaint(android.text.TextPaint)

Example 55 with ClickableSpan

use of android.text.style.ClickableSpan in project android_frameworks_base by ResurrectionRemix.

the class TextView method onTouchEvent.

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    if (mEditor != null) {
        mEditor.onTouchEvent(event);
        if (mEditor.mSelectionModifierCursorController != null && mEditor.mSelectionModifierCursorController.isDragAcceleratorActive()) {
            return true;
        }
    }
    final boolean superResult = super.onTouchEvent(event);
    /*
         * Don't handle the release after a long press, because it will move the selection away from
         * whatever the menu action was trying to affect. If the long press should have triggered an
         * insertion action mode, we can now actually show it.
         */
    if (mEditor != null && mEditor.mDiscardNextActionUp && action == MotionEvent.ACTION_UP) {
        mEditor.mDiscardNextActionUp = false;
        if (mEditor.mIsInsertionActionModeStartPending) {
            mEditor.startInsertionActionMode();
            mEditor.mIsInsertionActionModeStartPending = false;
        }
        return superResult;
    }
    final boolean touchIsFinished = (action == MotionEvent.ACTION_UP) && (mEditor == null || !mEditor.mIgnoreActionUpEvent) && isFocused();
    if ((mMovement != null || onCheckIsTextEditor()) && isEnabled() && mText instanceof Spannable && mLayout != null) {
        boolean handled = false;
        if (mMovement != null) {
            handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
        }
        final boolean textIsSelectable = isTextSelectable();
        if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(), getSelectionEnd(), ClickableSpan.class);
            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
        if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
            // Show the IME, except when selecting in read-only text.
            final InputMethodManager imm = InputMethodManager.peekInstance();
            viewClicked(imm);
            if (!textIsSelectable && mEditor.mShowSoftInputOnFocus) {
                handled |= imm != null && imm.showSoftInput(this, 0);
            }
            // The above condition ensures that the mEditor is not null
            mEditor.onTouchUpEvent(event);
            handled = true;
        }
        if (handled) {
            return true;
        }
    }
    return superResult;
}
Also used : InputMethodManager(android.view.inputmethod.InputMethodManager) ClickableSpan(android.text.style.ClickableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable)

Aggregations

ClickableSpan (android.text.style.ClickableSpan)62 View (android.view.View)25 Layout (android.text.Layout)20 TextPaint (android.text.TextPaint)19 TextView (android.widget.TextView)15 Spannable (android.text.Spannable)14 SpannableString (android.text.SpannableString)14 Paint (android.graphics.Paint)13 SpannableStringBuilder (android.text.SpannableStringBuilder)8 Intent (android.content.Intent)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 Context (android.content.Context)4 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)4 StyleSpan (android.text.style.StyleSpan)4 DialogInterface (android.content.DialogInterface)3 AlertDialog (android.support.v7.app.AlertDialog)3 ImageView (android.widget.ImageView)3 SpanInfo (org.chromium.ui.text.SpanApplier.SpanInfo)3 PendingIntent (android.app.PendingIntent)2 Uri (android.net.Uri)2