Android“格式化TextView的簡單方法

來源:互聯網
上載者:User
Wednesday, August 4, 2010

Easy method for formatting Android TextViews

Android TextViews don't have an easy method for changing the style of a substring of its text. You may have wanted to do something liketextView.setTextColor(Color.RED, 10, 20); in order to set the 10th to the 20th characters red. I'll show you
a method of making this easy to do; not just with colors, but with all sorts of styles.

Using regular HTML tags in strings

You do have a limited option to format your strings without any programmatic methods. In strings.xml, where you define your strings, you can use the simple HTML format tags <b><i>, and <u> for bold, italics, and underlining,
respectively. For example,<string name="text1">This text uses <b>bold</b> and <i>italics</i> by using inline tags such as <b> within the string file.</string>

A TextView with this string will appear as: This text uses bold and italics by using inline tags such as <b> within the string file.
Unfortunately, you cannot use any more HTML tags than that. In order to have some nice inline styles, we'll need to use CharacterStyles.
This class has many sub-classes, and allows you to do everything from changing the text appearance (like ForegroundColorSpan,
to more complicated things like making clickable links (ClickableSpan)
and images (ImageSpan). 
CharSequences

A quick note about how this works. Skip to the next section if you don't care.TextView, along with many other Android classes which use formatted text, don't just use simple Strings. They use CharSequences.
Get this: a CharSequence is more abstract than a String; a String is a sub-class of CharSequence. A CharSequencedefines a series of characters, such as a regular string, but it could
also define a series of characters with formatting, such as a SpannableString. Internally, what we will do to change the middle of a TextView's text is to add spans to its text. More precisely, we will add CharacterStyles to
the TextView's CharSequence (text), which is a SpannableString.
Format text dynamically

Here's a handy utility method which will take in some text, and return the text formatted. The key is to surround the text you want with tokens such as "##". The tokens will be removed in the returned result.
For example, if a TextView has its text set as "Hello, ##world##!" and you wantworld to be in red, call setSpanBetweenTokens("Hello ##world##!", "##", new ForegroundColorSpan(0xFFFF0000)); will return the
text Hello, world! with world in red. Note that you can send multiple spans as parameters to this method.

/**
 * Given either a Spannable String or a regular String and a token, apply
 * the given CharacterStyle to the span between the tokens, and also
 * remove tokens.
 * <p>
 * For example, {@code setSpanBetweenTokens("Hello ##world##!", "##",
 * new ForegroundColorSpan(0xFFFF0000));} will return a CharSequence
 * {@code "Hello world!"} with {@code world} in red.
 *
 *  @param text The text, with the tokens, to adjust.
 *  @param token The token string; there should be at least two instances
 *             of token in text.
 *  @param cs The style to apply to the CharSequence. WARNING: You cannot
 *            send the same two instances of this parameter, otherwise
 *            the second call will remove the original span.
 *  @return A Spannable CharSequence with the new style applied.
 *
 *  @see http://developer.android.com/reference/android/text/style/CharacterStyle.html
 */
public  static CharSequence setSpanBetweenTokens(CharSequence  text,
    String  token, CharacterStyle...  cs)
{
    // Start and end refer to the points where the span will apply
     int  tokenLen =  token.length();
     int  start =  text.toString().indexOf( token) +  tokenLen;
     int  end =  text.toString().indexOf( tokenstart);

    if (start > -1 && end > -1)
    {
        // Copy the spannable string to a mutable spannable string
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        for (CharacterStyle c : cs)
            ssb.setSpan(cstartend, 0);

        // Delete the tokens before and after the span
        ssb.delete(endend + tokenLen);
        ssb.delete(start - tokenLenstart);

        text = ssb;
    }

    return text;
}

Clickable spans

One of the spans you can set your text to is a ClickableSpan.
You may have added this and wondered why anything didn't happen when you clicked on the link. Well, you need to have one extra step to let Android know that there is a clickable link and it needs to be navigated to. You need to set a MovementMethod to
the TextView. You can investigate this further if you wish, or you can just see the sample code to make it work below (and it is also in the sample):


// Adapted from Linkify.addLinkMovementMethod(), to make links clickable.
//
MovementMethod  m =  textView.getMovementMethod();
if (( m ==  null) || !( m  instanceof LinkMovementMethod))
{
     textView.setMovementMethod(LinkMovementMethod.getInstance());
}

Sample Application

The sample application is a simple application with a few TextViews and a Button. The first TextView shows how you can set a TextView's text just by using HTML tags in strings.xml. The second TextView demonstrates
how to change text dynamically, using the above utility method. When the button is clicked, it will first set some text red using a ForegroundColorSpan. Second, it will set some text bold and italics using a StyleSpan. Third, it will
make a generic link by setting some text to blue, underlining it (UnderlineSpan), and then creating an onClick method which executes some custom code using a ClickableSpan. The final click demonstrates both a ForegroundColorSpan and
RelativeSizeSpan.

Project source code - formattedtext.zip (7.22
Kb)

Posted by Matt Quigley at 11:13
PM 

Email
ThisBlogThis!Share
to TwitterShare
to Facebook

Labels: UI
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.