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(mText.length() == 0) { |
// 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 instanceofSpannedString) |
if(source instanceofSpanned) |
returnnewSpannedString(source); |
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); |
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]
Customize the size of hint text in EditText in Android