2012-01-02 7 views
12

Tôi có EditText. Khi tôi nhấp vào nó, nó sẽ trở nên tập trung. Tôi sẽ nhập văn bản đầu vào được nhập vào EditText. Tôi muốn thực hiện một người nghe cho EditText, để khi tôi ngừng gõ, nó sẽ tự động lưu văn bản đó vào cơ sở dữ liệu thay vì có một nút. Làm thế nào để có một người nghe cho EditText để nghe rằng gõ được dừng lại hay không?Triển khai Trình theo dõi văn bản cho EditText

Trả lời

14

bộ EditText imeOption

editText.setImeOptions(EditorInfo.IME_ACTION_DONE); 

Bằng cách sử dụng một cái gì đó như thế này,

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if (actionId == EditorInfo.IME_ACTION_DONE) { 
      // Specify your database function here. 
      return true; 
     } 
     return false; 
    } 
}); 

Ngoài ra, bạn có thể sử dụng giao diện OnEditorActionListener để tránh các lớp bên trong vô danh.

+0

Bạn cũng có thể đóng bàn phím ảo sau khi Xong được nhấp bằng cách trả về false thay vì trả về true. – Youness

38

Hãy thử như thế này.

EditText et = (EditText)findViewById(R.id.editText); 
Log.e("TextWatcherTest", "Set text xyz"); 
et.setText("xyz"); 

et.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { } 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { } 
    @Override 
    public void afterTextChanged(Editable s) { 
     Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString()); 
    } 
}); 
+0

của bạn này sẽ là câu trả lời đúng. –

4

Thêm này để EditText

et1.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

      // TODO Auto-generated method stub 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      // TODO Auto-generated method stub 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

      // TODO Auto-generated method stub 
     } 
    });