在Android中输入EditText后如何隐藏键盘?

UMA*_*MAR 69 android android-edittext

我有一个EditText按钮与父母的底部对齐.

当我在其中输入文本并按下按钮以保存数据时,虚拟键盘不会消失.

任何人都可以指导我如何隐藏键盘吗?

小智 124

您可能还想在EditText中定义imeOptions.这样,按完成后键盘就会消失:

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>
Run Code Online (Sandbox Code Playgroud)

  • 只有在实现`onEditorAction([...])`时不消耗该事件时,这才会成立.返回"true"将阻止键盘正确隐藏. (33认同)
  • 按下"完成"或"搜索"以获取actionDone或actionSearch后,软键盘将解除,但您必须在onEditorActionListener中返回false. (6认同)
  • 这和`android:singleLine ="true"`. (6认同)

Rya*_*ord 119

这应该工作.

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 
Run Code Online (Sandbox Code Playgroud)

只要确保this.getCurrentFocus()不返回null,如果没有焦点则会返回null.

  • InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(),0); 现在工作...... (32认同)
  • 您无需使用此方法隐藏软键盘.请参阅下面的@angelrh以及在OnEditorActionListener中返回false的注释,以及从xml将编辑文本标记为singleLine = true. (2认同)

Ank*_*pra 23

   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;
            }
        });
Run Code Online (Sandbox Code Playgroud)

并在xml中

  android:imeOptions="actionDone"
Run Code Online (Sandbox Code Playgroud)


sum*_*dey 11

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)


小智 6

我没有看到任何人使用此方法:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

然后只需将焦点集中到editText即可:

editText.requestFocus();
Run Code Online (Sandbox Code Playgroud)


yoh*_*ohm 5

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;
        }
    });
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)