Có thể phát hiện khi Donephím của bàn phím ảo được nhấn không?
Có thể phát hiện khi Donephím của bàn phím ảo được nhấn không?
Câu trả lời:
Có, có thể:
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
Lưu ý rằng bạn sẽ phải nhập các thư viện sau:
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
Thông tin biên tập viên là lớp hữu ích nhất khi bạn phải xử lý bất kỳ loại đầu vào nào của người dùng trong ứng dụng Android của mình. Ví dụ: trong các thao tác đăng nhập / đăng ký / tìm kiếm, chúng ta có thể sử dụng nó để nhập liệu bằng bàn phím chính xác hơn. Một lớp thông tin trình soạn thảo mô tả một số thuộc tính cho đối tượng soạn thảo văn bản mà một phương thức nhập sẽ trực tiếp giao tiếp với nội dung văn bản chỉnh sửa.
Bạn có thể thử với IME_ACTION_DONE .
Hành động này thực hiện một Donehoạt động không có gì để nhập và IMEsẽ được đóng lại.
Sử dụng setOnEditorActionListener
EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
/* Write your logic here that will be executed when user taps next button */
handled = true;
}
return handled;
}
});
Sử dụng Butterknife, bạn có thể làm điều này
@OnEditorAction(R.id.signInPasswordText)
boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
/* Write your logic here that will be executed when user taps next button */
}
return false;
}
EditorInfo.IME_ACTION_SEARCHnút tìm kiếm ống kính để thay thế.