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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2342620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:28:56  来源:igfitidea点击:

how to hide keyboard after typing in EditText in android?

androidandroid-edittext

提问by UMAR

I have a EditTextand button aligned to parent's bottom.

我有一个EditText和按钮对齐到父母的底部。

When I enter text in it and press the button to save data, the virtual keyboard does not disappear.

当我在其中输入文本并按下按钮保存数据时,虚拟键盘并没有消失。

Can any one guide me how to hide the keyboard?

谁能指导我如何隐藏键盘?

回答by angelrh

You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:

您可能还想在 EditText 中定义 imeOptions。这样,一旦您按下“完成”,键盘就会消失:

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>

回答by Ryan Alford

This should work.

这应该有效。

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.

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

回答by Ankita Chopra

   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;
            }
        });

and in xml

并在 xml

  android:imeOptions="actionDone"

回答by sumit pandey

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

回答by Ofir

I did not see anyone using this method:

我没有看到有人使用这种方法:

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);
    }
});

And then just request focus to the editText:

然后只请求焦点到editText:

editText.requestFocus();

回答by yohm

Solution included in the EditText action listenner:

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;
        }
    });
    ...
    ...
}

回答by Shahadat Hossain

Just write down these two lines of code where enter option will work.

只需写下这两行代码,输入选项将起作用。

InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

回答by IsaiahJ

I found this because my EditText wasn't automatically getting dismissed on enter.

我发现这是因为我的 EditText 在输入时不会自动被解雇。

This was my original code.

这是我的原始代码。

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;
    }
});

I solved it by removing the line

我通过删除该行解决了它

return true;

after doing stuff when user presses enter.

当用户按下回车键时,在做一些事情之后。

Hope this helps someone.

希望这可以帮助某人。

回答by Alex Zaraos

you can create a singleton class for call easily like this:

您可以像这样轻松地创建一个用于调用的单例类:

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();
                }
            }
        });
    }
}

so, after can call in the activity how the next form:

所以,在活动中可以调用下一个形式后如何:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        KeyboardUtils.getInstance().hide(this);
    }

}

回答by Daniel

Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.

过去几天一直在努力解决这个问题,并找到了一个非常有效的解决方案。当在 EditText 之外的任何地方进行触摸时,软键盘会隐藏。

Code posted here: hide default keyboard on click in android

此处发布的代码:在 android 中单击时隐藏默认键盘