問(wèn)題描述
我知道 this/similar question 之前已被問(wèn)過(guò),但解決方案given 對(duì)我不起作用,所以我再次詢(xún)問(wèn).我嘗試了該答案中給出的解決方案,但我的 OnKeyListener 仍然從未在某些設(shè)備上被調(diào)用,尤其是那些具有股票操作系統(tǒng)的設(shè)備.當(dāng)editText中沒(méi)有字符時(shí),我需要檢測(cè)是否按下軟鍵盤(pán)的del鍵.這是我的代碼;
I know this/similar question has been asked before but the solution given is not working for me so I'm asking again. I tried the solution given in that answer but still my OnKeyListener is never being invoked on some devices, especially the ones with stock OS. I need to detect pressing of del key of soft keyboard when when there is no character in editText. Here is my code;
EditText et = (EditText) findViewById(R.id.et);
et.setOnKeyListener(new EditText.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("OnKeyListener", keyCode + " character(code) to send");
return false;
}
});
推薦答案
終于自己解決了,通過(guò)TextWatcher
實(shí)現(xiàn)這個(gè)功能.主要障礙是,即使 EditText
中沒(méi)有字符,或者至少最終用戶(hù)認(rèn)為那里沒(méi)有字符,我也需要檢測(cè)退格鍵.第一件事無(wú)法實(shí)現(xiàn),但我做了后一個(gè).以下是詳細(xì)的解決方案.
Finally solved myself by implementing this feature through TextWatcher
. The major hurdle was that, I needed to detect backspace press even when there is no character in EditText
or at least the end user perceives that there is no character there. The fist thing couldn't be achieved however I did the later one. Following is the detailed solution.
首先,我的 editText
中總是保留一個(gè)空格 ' ' 字符.
First of all, I always retained a space ' ' character in my editText
.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if(cs.toString().length() == 0)
editText.setText(" ");
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
@Override
public void afterTextChanged(Editable arg0) { }
});
然后我自定義了 EditText
來(lái)通知我每次光標(biāo)位置的變化.這個(gè)目的是通過(guò)重寫(xiě) EditText
的 onSelectionChanged
方法來(lái)實(shí)現(xiàn)的.我自定義的 EditText
是這樣的.
Then I customized EditText
to notify me for every cursor position change. This purpose is achieved by overriding onSelectionChanged
method of EditText
. My customized EditText
looks like this.
public class SelectionEnabledEditText extends EditText {
public SelectionEnabledEditText(Context context) {
super(context);
}
public SelectionEnabledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SelectionEnabledEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
if(onSelectionChangeListener != null)
onSelectionChangeListener.onSelectionChanged(selStart, selEnd);
}
public static interface OnSelectionChangeListener{
public void onSelectionChanged(int selStart, int selEnd);
}
private OnSelectionChangeListener onSelectionChangeListener;
public void setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener) {
this.onSelectionChangeListener = onSelectionChangeListener;
}
}
最后,在我的活動(dòng)中,我正在監(jiān)聽(tīng)光標(biāo)位置更改事件并在 editText
中重置我的光標(biāo)位置,如果它在必要的空格字符開(kāi)始處,即在第 0 個(gè)索引處,像這樣;
Finally, in my activity, I'm listening for cursor position changed event and resetting my cursor position in editText
if it's there at the necessary space charatcer start i.e. at 0th index, like this;
editText.setOnSelectionChangeListener(new SelectionEnabledEditText.OnSelectionChangeListener() {
@Override
public void onSelectionChanged(int selStart, int selEnd) {
if (selEnd == 0) {
if (editText.getText().toString().length() == 0)
editText.setText(" ");
editText.setSelection(1);
}
}
});
希望這對(duì)類(lèi)似情況有所幫助.歡迎提出改進(jìn)/優(yōu)化建議.
Hope this would be helpful in similar situations. Suggestions are welcomed for improvements/optimizations.
這篇關(guān)于EditText OnKeyListener 不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!