如何在不更改 EditText 中的文本大小的情况下更改提示文本大小

新手上路,请多包涵

我有一个 EditText 输入字段。我在其中添加了一个提示。现在我想更改提示文本的大小,但是当我这样做时,它也会影响文本的大小。请指导我如何分别更改提示和文本的大小,并为提示和文本赋予不同的字体。

 <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" />

原文由 dev90 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 480
2 个回答

提示和文字是排他性的,如果其中一个可见,另一个不可见。

因此,您只需更改 EditText 的属性,具体取决于它是否为空(提示可见)或不为空(文本可见)。

例如:

 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) {
    }
});

原文由 earthw0rmjim 发布,翻译遵循 CC BY-SA 3.0 许可协议

你可以在资源文件中设置它。

例如:

 <string name="hint"><font size="20">Hint!</font></string>

还有你的 XML:

 android:hint="@string/hint"

原文由 Filip Rosca 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题