我有一个位于父视图底部的EditText
和按钮。
当我在其中输入文本并按下保存数据的按钮时,虚拟键盘不会消失。
请问有人可以指导我如何隐藏键盘吗?
我有一个位于父视图底部的EditText
和按钮。
当我在其中输入文本并按下保存数据的按钮时,虚拟键盘不会消失。
请问有人可以指导我如何隐藏键盘吗?
你可能还想在EditText中定义imeOptions。这样,当你按下“完成”时,键盘就会消失:
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
onEditorAction([...])
时不消耗事件,才能保持这种情况。返回“true”将防止键盘正确隐藏。 - Eric Tobiasandroid:singleLine="true"
。 (说明:这句话比较短且缺乏上下文,我已经尽力保持原意进行翻译) - SimassingleLine
已被替换为maxLines=1
。应该可以正常工作。如果不能,请创建一个单独的问题。 - Simas这应该可以工作。
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
只需确保 this.getCurrentFocus() 不返回 null,否则如果没有焦点则会返回 null。
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do something, e.g. set your TextView here via .setText()
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
以及在XML中
android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
我没有看到任何人使用这种方法:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused)
keyboard.showSoftInput(editText, 0);
else
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
然后只需请求EditText获取焦点:
editText.requestFocus();
在EditText操作监听器中包含的解决方案:
public void onCreate(Bundle savedInstanceState) {
...
...
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
...
...
}
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
// Do stuff when user presses enter
return true;
}
return false;
}
});
return true;
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
您可以在顶部看到标记的答案。但是我使用了getDialog().getCurrentFocus()
,并且工作得很好。我发布这个答案,因为我无法在我的oncreatedialog中输入"this"
。
所以这就是我的答案。如果您尝试了标记的答案但没有成功,您可以简单地尝试这个:
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);