为了账号安全,请及时绑定邮箱和手机立即绑定

在用户输入时更改 TextInputLayout 的 Hint 值

在用户输入时更改 TextInputLayout 的 Hint 值

LEATH 2021-08-19 17:37:45
这是我的TextInputEditText内部TextInputLayout。您可以看到的提示设置为TextInputLayout。用户必须写255 个字符,否则帖子无效。我希望数字255随着用户键入而减少,当达到 0 时,提示应为空白。这是我的代码:private int charactersLeft = 256;在onCreate方法之外声明。这段代码在onCreate方法里面:final TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);final TextInputEditText postTextInput = findViewById(R.id.post_text_input);postTextInput.addTextChangedListener(new TextWatcher() {    @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 (charactersLeft == 0) {            postTextInputBase.setHint(null);        } else {            postTextInputBase.setHint((charactersLeft - 1) + " characters left");        }    }    @Override    public void afterTextChanged(Editable s) {    }});我也尝试设置charactersLeft为characterLeft -= 1但是,java不允许我这样做。那么,我该怎么做呢?
查看完整描述

3 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

要仅更改提示文本,您只需在焦点更改时更改它。


editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override

        public void onFocusChange(View v, boolean hasFocus) {

            if(hasFocus)

                editText.setHint("Enter name");

            else

                editText.setHint("Name");

        }

    });


查看完整回答
反对 回复 2021-08-19
  • 3 回答
  • 0 关注
  • 395 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信