Android 提示信息在文本框输入后消失

接上一篇接着写文本监听事件

上图

没错,是之前的图,还是之前的方法,只不过,这次不在afterTextChanged()里面写了,这次在onTextChanged()里面写,表示文本框正在改变时的监听。

在文本框未输入时,提示信息存在,当文本框输入时,提示信息消失,我就写了对第一个文本框的监听,如果需要监听两个文本框,就同时调用那个方法,或者直接参考我的上一篇文章:

首先,你要写一个TextView,并将提示信息写入

<TextView
        android:id="@+id/tv_pswlength"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="请输入6-12位数字+字母(不含空格)"
        android:textColor="@color/colorRed"
        android:layout_below="@+id/et_confirmpsw"
        android:layout_centerHorizontal="true"
        />

然后调用TextWatcher的方法

TextWatcher textWatcher = 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 (et_newpsw.getText().length()==0){
                    tv_pswlength.setText("请输入6-12位数字+字母(不含空格)");
                }else {
                    tv_pswlength.setText("");
                }

            }

           
            @Override
            public void afterTextChanged(Editable s) {
               
            }
        };
        et_newpsw.addTextChangedListener(textWatcher);
     

关键代码就是这样,其他就是控件的定义和绑定,就不贴出来了。 

 如果有啥错误或者不懂的 直接评论,尽吾所能解答。

猜你喜欢

转载自blog.csdn.net/qq_40480758/article/details/84101043
今日推荐