問題描述
我需要檢測 EditText
中軟鍵盤或硬鍵盤上的每個(gè)按鍵.我只需要在按下字符時(shí)一次發(fā)送一個(gè)字符,不需要最終的文本字符串.
I need to detect every key press on either a soft or hard keyboard in EditText
. I just need to send the characters one at a time as they are pressed and don't need the final text string.
我曾嘗試將 EditText
與 onKeyPress
一起使用,但我在這里遇到了問題,即無法使用軟鍵盤和 TextWatcher
進(jìn)行按鍵操作不是一個(gè)好的選擇,因?yàn)槲倚枰總€(gè)按鍵.是否有任何解決方案可以知道所有按鍵(包括返回、移位、回車...也)?
I have tried using an EditText
with onKeyPress
, but I ran into the problems here with not getting key presses with soft keyboards and TextWatcher
isn't a good option because I need each key press.
Is there any solution to know all the keypresses (including back, shift, enter... also)?
推薦答案
如果你有一個(gè)EditText
,那么你可以使用TextWatcher
接口.在我的代碼中,search_edit
是一個(gè) EditText
.
If you have an EditText
, then you can use the TextWatcher
interface. In my code, search_edit
is an EditText
.
search_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//here is your code
myadapter.getFilter().filter(s);
listview.setAdapter(myadapter);
}
@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
}
});
這篇關(guān)于如何知道 EditText 中的按鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!