Android 提示資訊在文字框輸入後消失
阿新 • • 發佈:2018-12-18
接上一篇接著寫文字監聽事件
上圖
沒錯,是之前的圖,還是之前的方法,只不過,這次不在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);
關鍵程式碼就是這樣,其他就是控制元件的定義和繫結,就不貼出來了。
如果有啥錯誤或者不懂的 直接評論,盡吾所能解答。