我知道 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模板网!
EditText:禁用文本选择处理程序单击事件上的粘贴EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本选择处理程序单击事件上的粘贴/替换菜
2.3 上带有完成 SoftInput 操作标签的多行 EditTextMultiline EditText with Done SoftInput Action Label on 2.3(2.3 上带有完成 SoftInput 操作标签的多行 EditText)
如何在 Android 中检测向左或向右滑动?How to detect the swipe left or Right in Android?(如何在 Android 中检测向左或向右滑动?)
防止在Android中的屏幕旋转对话框解除Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋转对话框解除)
如何处理 ImeOptions 的完成按钮点击?How do I handle ImeOptions#39; done button click?(如何处理 ImeOptions 的完成按钮点击?)
您如何将 EditText 设置为仅接受 Android 中的数值How do you set EditText to only accept numeric values in Android?(您如何将 EditText 设置为仅接受 Android 中的数值?)