Customize the size of hint text in EditText in Android

Source: Internet
Author: User

"Original" Customizing the size of hint text in EditText in Android2014/10/20 Hein Post reply

Today, colleagues in the work encountered a problem, that is, the text in the EditText after the size, hint text because too long caused in the edittext can not complete display, so asked whether there is a separate option to set the hint text size. There is no introduction to this in the Internet. So I looked at the source of the next TextView (EditText inherited from TextView), found some clues, as follows:

publicfinal voidsetHint(CharSequence hint) {
    mHint = TextUtils.stringOrSpannedString(hint);
    if(mLayout != null) {
        checkForRelayout();
    }
    if(mText.length() == 0) {
        invalidate();
    }
    // Invalidate display list if hint is currently used
    if(mEditor != null && mText.length() == 0&& mHint != null) {
        mEditor.invalidateTextDisplayList();
    }
}

At the beginning of the method is the conversion of the hint text. Since hint is a charsequence type, it is possible to add some custom properties, and we'll look at textutils.stringorspannedstring this method:

publicstaticCharSequence stringOrSpannedString(CharSequence source) {
    if(source == null)
        returnnull;
    if(source instanceofSpannedString)
        returnsource;
    if(source instanceofSpanned)
        returnnewSpannedString(source);
    returnsource.toString();
}

So the question is, can we keep the custom properties of the text as long as the incoming hint is a spannedstring or spanned type? The answer is YES! Directly on the code:

EditText editText = (EditText) rootView.findViewById(R.id.et);
// 新建一个可以添加属性的文本对象
SpannableString ss = newSpannableString("喝酒就要喝一斤!");
// 新建一个属性对象,设置文字的大小
AbsoluteSizeSpan ass = newAbsoluteSizeSpan(8, true);
// 附加属性到文本
ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 设置hint
editText.setHint(newSpannedString(ss)); // 一定要进行转换,否则属性会消失

Note The final step, be sure to convert, the type is not converted to a string object, so the custom amount property will be lost.

The following are the final effects:


In addition to changing the size of the hint, other properties can be changed, the specific spaned type can refer to this link: Android in various types of span comprehensive system research

Note: This article belongs to [email protected] Jiangnan Yi Original, reproduced please indicate the source http://www.jiangnane.com/

e-mail: [email protected]

share to:0

Androidedittexthintsizetextview

Article Navigation

Previous article

Customize the size of hint text in EditText in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.