問題描述
我知道 this/similar question 之前已被問過,但解決方案given 對我不起作用,所以我再次詢問.我嘗試了該答案中給出的解決方案,但我的 OnKeyListener 仍然從未在某些設備上被調用,尤其是那些具有股票操作系統的設備.當editText中沒有字符時,我需要檢測是否按下軟鍵盤的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;
}
});
推薦答案
終于自己解決了,通過TextWatcher
實現這個功能.主要障礙是,即使 EditText
中沒有字符,或者至少最終用戶認為那里沒有字符,我也需要檢測退格鍵.第一件事無法實現,但我做了后一個.以下是詳細的解決方案.
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
中總是保留一個空格 ' ' 字符.
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
來通知我每次光標位置的變化.這個目的是通過重寫 EditText
的 onSelectionChanged
方法來實現的.我自定義的 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;
}
}
最后,在我的活動中,我正在監聽光標位置更改事件并在 editText
中重置我的光標位置,如果它在必要的空格字符開始處,即在第 0 個索引處,像這樣;
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);
}
}
});
希望這對類似情況有所幫助.歡迎提出改進/優化建議.
Hope this would be helpful in similar situations. Suggestions are welcomed for improvements/optimizations.
這篇關于EditText OnKeyListener 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!