简体   繁体   English

EditText.setHint 在其中包含内容时不起作用

[英]EditText.setHint doesn't work when it has content in it

I want to add a clear function to a button.我想在按钮上添加一个清晰的 function 。 I have written this code,我写了这段代码,

clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            name.setHint("Enter name");
            mail.setHint("Enter mail");
            num.setHint("Enter num");
        }
    });

It works when the field is empty, but does not if field has content in it.它在字段为空时有效,但如果字段中有内容则无效。

Thank you for your time.感谢您的时间。 It's not a TextInputLayout.它不是 TextInputLayout。 Works well with EditText.setText.与 EditText.setText 配合得很好。

The hint will not show when there is text inside a text field.当文本字段中有文本时,提示不会显示。 You can clear text like below:您可以像下面这样清除文本:

clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            name.setHint("Enter name");
            name.setText("");

            mail.setHint("Enter mail");
            mail.setText("");

            num.setHint("Enter num");
            num.setText("");
        }
    });

The hint will only show when the content is empty.提示仅在内容为空时显示。 So you may clear your content and set hint at the same time.因此,您可以同时清除您的内容并设置提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM