android 中 TextView.setText(CharSequence text, BufferType type)的了解

我们经常用TextView.setText(“”);的方法,但是TextView有一个另外一个俩个入参,之前不太知道其用法,现在记录一下

效果图

《android 中 TextView.setText(CharSequence text, BufferType type)的了解》 image.png

核心代码
/**
     *实现同一个TextView设置不同的字体风格
     * @param context
     * @param textView
     * @param firstValue
     * @param secondValue
     */
    public static void initText(Context context,TextView textView,String firstValue,String secondValue) {
        SpannableString ss = new SpannableString(firstValue + secondValue);
        ss.setSpan(new TextAppearanceSpan(context, R.style.style_gray_24_text), 0, firstValue.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new TextAppearanceSpan(context, R.style.style_black_2_24_text), firstValue.length(),
                (firstValue + secondValue).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(ss, TextView.BufferType.SPANNABLE);
    }
setText(CharSequence text, BufferType type)的源码
  /**
     * Sets the text that this TextView is to display (see
     * {@link #setText(CharSequence)}) and also sets whether it is stored
     * in a styleable/spannable buffer and whether it is editable.
     *
     * @attr ref android.R.styleable#TextView_text
     * @attr ref android.R.styleable#TextView_bufferType
     */
    public void setText(CharSequence text, BufferType type) {
        setText(text, type, true, 0);

        if (mCharWrapper != null) {
            mCharWrapper.mChars = null;
        }
    }
setText(CharSequence text, BufferType type)的翻译
/ * *
*设置此TextView将显示的文本(参见
* {@link # setText(CharSequence)},并设置是否存储
*在可编辑的/可扫描的缓冲区中,以及是否可编辑。
*
* @attr ref android.R.styleable # TextView_text
* @attr ref android.R.styleable # TextView_bufferType
* /
参考资料

1、http://blog.sina.com.cn/s/blog_5da93c8f01012pyp.html
2、http://blog.csdn.net/u012702547/article/details/49895157

    原文作者:追梦小乐
    原文地址: https://www.jianshu.com/p/9019d8cb1f1f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞