Android 当“编辑”时,如何失去编辑文本的焦点;“完成”;是否按下软键盘上的按钮?
我的xml中有7个编辑文本框。我正在使用OnFocusChangeListener读取edittext中的值,并使用该值进行计算。我想使我的edittext在单击软键盘中的“完成”按钮时失去焦点。这样我就可以在edittext中获取值 从软键盘单击“完成”按钮时,调用Android 当“编辑”时,如何失去编辑文本的焦点;“完成”;是否按下软键盘上的按钮?,android,android-edittext,android-softkeyboard,Android,Android Edittext,Android Softkeyboard,我的xml中有7个编辑文本框。我正在使用OnFocusChangeListener读取edittext中的值,并使用该值进行计算。我想使我的edittext在单击软键盘中的“完成”按钮时失去焦点。这样我就可以在edittext中获取值 从软键盘单击“完成”按钮时,调用clearFocus的EditText方法以失去焦点。按如下方式操作: editText.setOnEditorActionListener(new OnEditorActionListener() { @O
clearFocus
的EditText
方法以失去焦点。按如下方式操作:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
editText.clearFocus();
}
return false;
}
});
还可以在edittext xml中添加
android:imeOptions=“actionDone”
如果有人遇到这个问题,想知道如何在他们的活动中一次取消对所有内容的关注,他们可以将的焦点设置在父布局上(或任何布局上)
只是一个更完整的答案
// This will change the ‘RETURN’ button in your the EditText’s soft keyboard to a ‘DONE’ button.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
// Use the InputMethodManager to close the soft keyboard
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});
科特林,这对我很有用:
main.clearFocus()
main是根约束如果没有其他可聚焦视图,或者如果同一视图是从顶部开始的第一个可以聚焦的视图,则clearFocus可能无法工作。文档注释:当一个视图清除焦点时,框架试图从顶部将焦点赋予第一个可聚焦视图。因此,如果此视图是从顶部开始的第一个可以获得焦点的视图,那么所有与清除焦点相关的回调都将被调用,之后框架将对此视图给予焦点。使用
android:imeOptions=“actionSend”
将不会清除焦点,即使调用editText.clearFocus()
确保使用android:imeOptions=“actionDone”这会清除焦点,但会覆盖标准行为以隐藏键盘。first EditText不适用于我,它会在请求后获取焦点。
main.clearFocus()