Search in sources :

Example 11 with URLSpan

use of android.text.style.URLSpan in project MarqueeView by sfsheng0322.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    marqueeView = (MarqueeView) findViewById(R.id.marqueeView);
    marqueeView1 = (MarqueeView) findViewById(R.id.marqueeView1);
    marqueeView2 = (MarqueeView) findViewById(R.id.marqueeView2);
    marqueeView3 = (MarqueeView) findViewById(R.id.marqueeView3);
    marqueeView4 = (MarqueeView) findViewById(R.id.marqueeView4);
    List<CharSequence> list = new ArrayList<>();
    SpannableString ss1 = new SpannableString("1、MarqueeView项目");
    ss1.setSpan(new ForegroundColorSpan(Color.RED), 2, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss1);
    SpannableString ss2 = new SpannableString("2、GitHub:sfsheng0322");
    ss2.setSpan(new ForegroundColorSpan(Color.GREEN), 9, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss2);
    SpannableString ss3 = new SpannableString("3、个人博客:sunfusheng.com");
    ss3.setSpan(new URLSpan("http://sunfusheng.com/"), 7, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    list.add(ss3);
    list.add("4、孙福生微博,请粉哦");
    marqueeView.startWithList(list);
    marqueeView1.startWithText(getString(R.string.marquee_texts));
    marqueeView2.startWithText(getString(R.string.marquee_texts));
    marqueeView3.startWithText(getString(R.string.marquee_texts));
    marqueeView4.startWithText(getString(R.string.marquee_text));
    marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {

        @Override
        public void onItemClick(int position, TextView textView) {
            Toast.makeText(getApplicationContext(), textView.getText() + "", Toast.LENGTH_SHORT).show();
        }
    });
    marqueeView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() {

        @Override
        public void onItemClick(int position, TextView textView) {
            Toast.makeText(getApplicationContext(), String.valueOf(marqueeView1.getPosition()) + ". " + textView.getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : SpannableString(android.text.SpannableString) MarqueeView(com.sunfusheng.marqueeview.MarqueeView) ForegroundColorSpan(android.text.style.ForegroundColorSpan) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) URLSpan(android.text.style.URLSpan)

Example 12 with URLSpan

use of android.text.style.URLSpan in project platform_frameworks_base by android.

the class Editor method selectCurrentWord.

/**
     * Adjusts selection to the word under last touch offset. Return true if the operation was
     * successfully performed.
     */
private boolean selectCurrentWord() {
    if (!mTextView.canSelectText()) {
        return false;
    }
    if (needsToSelectAllToSelectWordOrParagraph()) {
        return mTextView.selectAllText();
    }
    long lastTouchOffsets = getLastTouchOffsets();
    final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
    final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
    // Safety check in case standard touch event handling has been bypassed
    if (minOffset < 0 || minOffset > mTextView.getText().length())
        return false;
    if (maxOffset < 0 || maxOffset > mTextView.getText().length())
        return false;
    int selectionStart, selectionEnd;
    // If a URLSpan (web address, email, phone...) is found at that position, select it.
    URLSpan[] urlSpans = ((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class);
    if (urlSpans.length >= 1) {
        URLSpan urlSpan = urlSpans[0];
        selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);
        selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);
    } else {
        // FIXME - We should check if there's a LocaleSpan in the text, this may be
        // something we should try handling or checking for.
        final WordIterator wordIterator = getWordIterator();
        wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);
        selectionStart = wordIterator.getBeginning(minOffset);
        selectionEnd = wordIterator.getEnd(maxOffset);
        if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE || selectionStart == selectionEnd) {
            // Possible when the word iterator does not properly handle the text's language
            long range = getCharClusterRange(minOffset);
            selectionStart = TextUtils.unpackRangeStartFromLong(range);
            selectionEnd = TextUtils.unpackRangeEndFromLong(range);
        }
    }
    Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
    return selectionEnd > selectionStart;
}
Also used : WordIterator(android.text.method.WordIterator) URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 13 with URLSpan

use of android.text.style.URLSpan in project XobotOS by xamarin.

the class LinkSpec method applyLink.

private static final void applyLink(String url, int start, int end, Spannable text) {
    URLSpan span = new URLSpan(url);
    text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Also used : URLSpan(android.text.style.URLSpan)

Example 14 with URLSpan

use of android.text.style.URLSpan in project XobotOS by xamarin.

the class TextView method onCreateContextMenu.

@Override
protected void onCreateContextMenu(ContextMenu menu) {
    super.onCreateContextMenu(menu);
    boolean added = false;
    mContextMenuTriggeredByKey = mDPadCenterIsDown || mEnterKeyIsDown;
    // Problem with context menu on long press: the menu appears while the key in down and when
    // the key is released, the view does not receive the key_up event.
    // We need two layers of flags: mDPadCenterIsDown and mEnterKeyIsDown are set in key down/up
    // events. We cannot simply clear these flags in onTextContextMenuItem since
    // it may not be called (if the user/ discards the context menu with the back key).
    // We clear these flags here and mContextMenuTriggeredByKey saves that state so that it is
    // available in onTextContextMenuItem.
    mDPadCenterIsDown = mEnterKeyIsDown = false;
    MenuHandler handler = new MenuHandler();
    if (mText instanceof Spanned && hasSelectionController()) {
        long lastTouchOffset = getLastTouchOffsets();
        final int selStart = extractRangeStartFromLong(lastTouchOffset);
        final int selEnd = extractRangeEndFromLong(lastTouchOffset);
        URLSpan[] urls = ((Spanned) mText).getSpans(selStart, selEnd, URLSpan.class);
        if (urls.length > 0) {
            menu.add(0, ID_COPY_URL, 0, com.android.internal.R.string.copyUrl).setOnMenuItemClickListener(handler);
            added = true;
        }
    }
    // populates the menu AFTER this call.
    if (menu.size() > 0) {
        menu.add(0, ID_SELECTION_MODE, 0, com.android.internal.R.string.selectTextMode).setOnMenuItemClickListener(handler);
        added = true;
    }
    if (added) {
        menu.setHeaderTitle(com.android.internal.R.string.editTextMenuTitle);
    }
}
Also used : URLSpan(android.text.style.URLSpan) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 15 with URLSpan

use of android.text.style.URLSpan in project WordPress-Android by wordpress-mobile.

the class WPHtml method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end, int nl, boolean last) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<strong>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<em>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof WPUnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<strike>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof MediaGalleryImageSpan) {
                out.append(getGalleryShortcode((MediaGalleryImageSpan) style[j]));
            } else if (style[j] instanceof WPImageSpan && ((WPImageSpan) style[j]).getMediaFile().getMediaId() != null) {
                out.append(getContent((WPImageSpan) style[j]));
            } else if (style[j] instanceof WPImageSpan) {
                out.append("<img src=\"");
                out.append(((WPImageSpan) style[j]).getSource());
                out.append("\" android-uri=\"" + ((WPImageSpan) style[j]).getImageSource().toString() + "\"");
                out.append(" />");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("<font size =\"");
                out.append(((AbsoluteSizeSpan) style[j]).getSize() / 6);
                out.append("\">");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("<font color =\"#");
                String color = Integer.toHexString(((ForegroundColorSpan) style[j]).getForegroundColor() + 0x01000000);
                while (color.length() < 6) {
                    color = "0" + color;
                }
                out.append(color);
                out.append("\">");
            }
        }
        processWPImage(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</font>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</font>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</strike>");
            }
            if (style[j] instanceof WPUnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</strong>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</em>");
                }
            }
        }
    }
    String p = last ? "" : "</p>\n<p>";
    if (nl == 1) {
        out.append("<br>\n");
    } else if (nl == 2) {
        out.append(p);
    } else {
        for (int i = 2; i < nl; i++) {
            out.append("<br>");
        }
        out.append(p);
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) WPUnderlineSpan(org.wordpress.android.util.helpers.WPUnderlineSpan) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Aggregations

URLSpan (android.text.style.URLSpan)134 SpannableString (android.text.SpannableString)36 Spannable (android.text.Spannable)22 SpannableStringBuilder (android.text.SpannableStringBuilder)21 TextPaint (android.text.TextPaint)21 Spanned (android.text.Spanned)20 TextView (android.widget.TextView)19 ForegroundColorSpan (android.text.style.ForegroundColorSpan)16 StyleSpan (android.text.style.StyleSpan)16 UnderlineSpan (android.text.style.UnderlineSpan)14 Paint (android.graphics.Paint)13 ImageSpan (android.text.style.ImageSpan)13 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)13 View (android.view.View)13 Intent (android.content.Intent)12 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)12 BackgroundColorSpan (android.text.style.BackgroundColorSpan)12 ClickableSpan (android.text.style.ClickableSpan)12 SubscriptSpan (android.text.style.SubscriptSpan)12