TextUtils

http://developer.android.com/reference/android/text/TextUtils.html


之前没有关注过 TextUtils 这个类,主要用到的就是很简单的方法, 比如 empty   equals 简单的方法

后面有时间了会对一些特殊的方法,源码解读


1.TextUtils.join("|" ,List<String>)   -----   string x =  " a | b | c "

  Spanny  mSpanny1 = new Spanny(mWords1).findAndSpan("关键的", new Spanny.GetSpan() {
            @Override
            public Object getSpan() {
                return new BackgroundColorSpan(Color.RED);
            }
        });



3. commaEllipsize

TextUtils.commaEllipsize("a b c d e f g h i g", mTextView.getPaint(), mScreenWidth, "1 more", "%d more");
"a b c d 1 more"



4.concat
TextUtils.concat(mSpanny1, mSpanny2);



5.copySpansFrom
TextUtils.copySpansFrom(mSpanny1 , start , end , Object.class, ss,  destoff);


6.dumpSpans
TextUtils.dumpSpans(mSpanny1, new LogPrinter(Log.INFO, "kenneth"), "yo");
打印cs中的Span,prefix会在打印每个Span开始出现。printer,可以用LogPrinter,这样就可以打印在 log 里。


7.ellipsize
public static CharSequence ellipsize(CharSequence text,TextPaint paint,
                                         float avail, TruncateAt where,
                                         boolean preserveLength,
                                         EllipsizeCallback callback)

相当于 TextView 的 xml 中ellipsize,这里可以回调省略范围的 index,同时也可用通过preserveLength,设置返回的 CharSequence 的长度为原始长度还是省略后的长度,这里利用的”零宽不换行空格符”来占位,where参数可以用TextUtils.TruncateAt.START,TextUtils.TruncateAt.END,TextUtils.TruncateAt.MIDDLE和TextUtils.TruncateAt.MARQUEE,分别是开始,结尾,中间和跑马灯。

CharSequence ellipsize = TextUtils.ellipsize(mWords2, mTextView.getPaint(), mScreenWidth, TextUtils.TruncateAt.START, true, new TextUtils.EllipsizeCallback() {
                    @Override
                    public void ellipsized(int i, int i1) {
                        Log.i(TAG, "被省略范围 " + i + " " + i1);
                    }
                });


8.expandTemplate

public static CharSequence expandTemplate(CharSequence template, CharSequence... values) 
替换template当中的^1`` ^2 等为values中相应的值,注意:不能超过9个,9个以上会抛出异常。

String template = "This is a ^1 of the ^2 broadcast ^3.";
                mTextView.setText(template);
                CharSequence expandTemplate = TextUtils.expandTemplate(template, "test", "emergency", "system");



9.getCapsMode

public static int getCapsMode(CharSequence cs, int off, int reqModes)
这个方法不知道怎么用,求留言赐教。


10. getChars

public static void getChars(CharSequence s, int start, int end,
                                char[] dest, int destoff)

char[] buffer = new char[5];

TextUtils.getChars(mWords1, 2, 4, buffer, 0);


11.
getLayoutDirectionFromLocale

public static int getLayoutDirectionFromLocale(Locale locale)

根据传入的 local获取当前的阅读习惯(例如,汉语习惯是左到右,希伯来语是右到左),
具体可以看这篇文章,http://droidyue.com/blog/2014/07/07/support-rtl-in-android/index.html



12. getReverse  翻转start到end的字符串。

public static CharSequence getReverse(CharSequence source, int start, int end)



13.getTrimmedLength

public static int getTrimmedLength(CharSequence s)
trim 后的字符串长度。


14. htmlEncode

public static String htmlEncode(String s)
encode html ,注意是 html 。


15. indexOf
public static int indexOf(CharSequence s, CharSequence needle,
                              int start, int end)


16.

isDigitsOnly

public static boolean isDigitsOnly(CharSequence str)

判断是否仅有数字。

17.lastIndexOf

public static int lastIndexOf(CharSequence s, char ch)


18. regionMatches  比对两个字符串 offset到 len 的长度的字符串是否相同
public static boolean regionMatches(CharSequence one, int toffset,
                                        CharSequence two, int ooffset,
                                        int len) {


19.replace

public static CharSequence replace(CharSequence template,
                                       String[] sources,
                                       CharSequence[] destinations)
替换功能,注意:只会替换第一个出现的。

例子:

mOutputView.setText(TextUtils.replace(mWords1,new String[]{"很长","的","."},new String[]{"very long","of","。"}));



20. split

public static String[] split(String text, String expression)

分割字符串。
例子:

mOutputView.setText(TextUtils.join("--",TextUtils.split(mWords2,",")));






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值