我有一个EditText
和按钮对齐到父母的底部。
当我在其中输入文本并按下按钮保存数据时,虚拟键盘不会消失。
谁能指导我如何隐藏键盘?
您可能还想在 EditText 中定义 imeOptions。这样,一旦您按下完成,键盘就会消失:
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
这应该有效。
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
只要确保 this.getCurrentFocus() 不返回 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 在输入时没有自动被解雇。
这是我的原始代码。
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);
过去几天一直在努力解决这个问题,并找到了一个非常有效的解决方案。当在 EditText 之外的任何地方进行触摸时,软键盘将被隐藏。
此处发布的代码:在 android 中单击时隐藏默认键盘
您可以在顶部看到标记的答案。但我使用getDialog().getCurrentFocus()
并且工作得很好。我发布此答案是因为我无法输入"this"
我的 oncreatedialog。
所以这就是我的答案。如果您尝试了标记答案但没有成功,您可以简单地试试这个:
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
您可以像这样轻松地创建一个单例类进行调用:
public class KeyboardUtils {
private static KeyboardUtils instance;
private InputMethodManager inputMethodManager;
private KeyboardUtils() {
}
public static KeyboardUtils getInstance() {
if (instance == null)
instance = new KeyboardUtils();
return instance;
}
private InputMethodManager getInputMethodManager() {
if (inputMethodManager == null)
inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
return inputMethodManager;
}
@SuppressWarnings("ConstantConditions")
public void hide(final Activity activity) {
new Handler().post(new Runnable() {
@Override
public void run() {
try {
getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
}
所以,在活动中可以调用下一个表单后如何:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
KeyboardUtils.getInstance().hide(this);
}
}
我使用这种方法从编辑文本中删除键盘:
public static void hideKeyboard(Activity activity, IBinder binder) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (binder != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
inputManager.showSoftInputFromInputMethod(binder, 0);
}
}
}
而这种从活动中删除键盘的方法(在某些情况下不起作用 - 例如,当edittext,到它是绑定键盘时,失去焦点,它不会起作用。但对于其他情况,它工作得很好,而且你没有关心持有键盘的元素)。
public static void hideKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
int klavStat = 1; // for keyboard soft/hide button
int inType; // to remeber your default keybort Type
编辑器 - 是 EditText 字段
/// metod for onclick button ///
public void keyboard(View view) {
if (klavStat == 1) {
klavStat = 0;
inType = editor.getInputType();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
editor.setInputType(InputType.TYPE_NULL);
editor.setTextIsSelectable(true);
} else {
klavStat = 1;
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
editor.setInputType(inType);
}
}
如果您有另一个 EditText 字段,则需要注意焦点变化。
就我而言,为了在按下“发送按钮”时隐藏键盘,我使用了接受的答案,但将上下文更改为 getApplication 并添加了 getWindow()。
InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
editText.setInputType(InputType.TYPE_NULL);