如何在不更改EditText中的文本大小的情况下更改提示文本大小 [英] How to change hint text size without changing the text size in EditText
问题描述
我有一个EditText
输入字段.我在其中添加了一个提示.现在,我想更改提示文本的大小,但是当我这样做时,它也会影响文本的大小.请指导我如何分别更改提示和文本的大小,并为提示和文本提供不同的字体.
I have a EditText
input field. I have added a hint in it. Now i want to change the size of hint text, but when i do this, it also effects the size of the text. Kindly guide me how to change the size of hint and text separately, and give different fonts to both the hint and the text.
<EditText
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:textSize="12sp"
android:textColor="#ffffff"
android:fontFamily="sans-serif-light"
android:hint="MM/YY"
android:textColorHint="@color/white" />
推荐答案
提示和文本是互斥的,如果其中一个可见,则另一个则不可见.
The hint and the text are exclusive, if one of them is visible, the other one is not.
因此,您可以根据EditText
的属性是否为空(提示可见)或不更改(文本可见)来更改其属性.
Because of this, you could just change the attributes of your EditText
depending on if it's empty (the hint is visible) or not (the text is visible).
例如:
final EditText editText = (EditText) findViewById(R.id.yourEditText);
editText.addTextChangedListener(new TextWatcher() {
boolean hint;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() == 0) {
// no text, hint is visible
hint = true;
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
editText.setTypeface(Typeface.createFromAsset(getAssets(),
"hintFont.ttf")); // setting the font
} else if(hint) {
// no hint, text is visible
hint = false;
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
editText.setTypeface(Typeface.createFromAsset(getAssets(),
"textFont.ttf")); // setting the font
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
这篇关于如何在不更改EditText中的文本大小的情况下更改提示文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!