標籤:按鈕 listener string sdn enter public lin and gravity
為了實現像qq或者輸入框的效果,當在
EditText輸入字串時發送按鈕顯示,當輸入框字元消除掉時按鈕改變。所以這時候我就要用到addTextChangedListener 用它來監聽使用者輸入狀態。可以在監聽中改變使用者輸入的內容或者提示使用者輸入內容不合法等等
- EditText editText = (EditText)findViewById(R.id.monitor_edit_text0);
- editText.addTextChangedListener(new TextWatcher() {
-
- @Override
- public void onTextChanged(CharSequence text, int start, int before, int count) {
- //text 輸入框中改變後的字串資訊
- //start 輸入框中改變後的字串的起始位置
- //before 輸入框中改變前的字串的位置 預設為0
- //count 輸入框中改變後的一共輸入字串的數量
- textView1.setText("輸入後字串 [ " + text.toString() + " ] 起始游標 [ " + start + " ] 輸入數量 [ " + count+" ]");
-
- }
-
- @Override
- public void beforeTextChanged(CharSequence text, int start, int count,int after) {
- //text 輸入框中改變前的字串資訊
- //start 輸入框中改變前的字串的起始位置
- //count 輸入框中改變前後的字串改變數量一般為0
- //after 輸入框中改變後的字串與起始位置的位移量
- System.out.println(text.toString());
- textView0.setText("輸入前字串 [ " + text.toString() + " ]起始游標 [ " + start + " ]結束位移量 [" + after + " ]");
- }
-
- @Override
- public void afterTextChanged(Editable edit) {
- //edit 輸入結束呈現在輸入框中的資訊
- textView2.setText("輸入結束後的內容為 [" + edit.toString()+" ] 即將顯示在螢幕上");
- }
- });
Android開發之EditText 詳解(addTextChangedListener監聽使用者輸入狀態)