首页 文章

关闭按钮上的虚拟键盘

提问于
浏览
123

我有一个带有 EditTextActivity ,一个按钮和一个 ListView . 目的是在 EditText 中键入搜索屏幕,按下按钮并使搜索结果填充此列表 .

这一切都很完美,但虚拟键盘表现得很奇怪 .

如果我单击 EditText ,我会得到虚拟键盘 . 如果我单击虚拟键盘上的"Done"按钮,它就会消失 . 但是,如果在单击虚拟键盘上的"Done"之前单击我的搜索按钮,则虚拟键盘会停留,我无法摆脱它 . 单击"Done"按钮不会关闭键盘 . 它将"Done"按钮从"Done"更改为箭头并保持可见 .

谢谢你的帮助

14 回答

  • 2
    View view = this.getCurrentFocus();
    if (view != null) {
    InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
    
  • 286
    InputMethodManager inputManager = (InputMethodManager)
                                      getSystemService(Context.INPUT_METHOD_SERVICE); 
    
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                         InputMethodManager.HIDE_NOT_ALWAYS);
    

    我把它放在 onClick(View v) 事件之后 .

    你需要导入 android.view.inputmethod.InputMethodManager ;

    单击按钮时键盘会隐藏 .

  • 0
    mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                          InputMethodManager.RESULT_UNCHANGED_SHOWN);
                return true;
            }
            return false;
        }
    });
    
  • -3

    Use Below Code

    your_button_id.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try  {
                InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            } catch (Exception e) {
    
            }
        }
    });
    
  • 54

    您应该为EditView实现 OnEditorActionListener

    public void performClickOnDone(EditView editView, final View button){
        textView.setOnEditorActionListener(new OnEditorActionListener() {
    
            @Override
            public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
                hideKeyboard();
                button.requestFocus();
                button.performClick();
                return true;
            }
        });
    

    你隐藏键盘:

    public void hideKeybord(View view) {
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
    

    您还应该使用 onClickListener 在按钮中触发键盘隐藏

    现在单击虚拟键盘上的“完成”按钮将执行相同操作 - 隐藏键盘并执行单击操作 .

  • 13

    在按钮单击事件中添加以下代码:

    InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    
  • 0

    由于您只有一个编辑文本,因此只需在按钮单击内调用该编辑文本的操作,其余部分由系统处理 . 如果您有多个edittext,那么这将不会那么高效,因为您必须首先获得有针对性的edittext . 但在你的情况下,它将完美地工作

    myedittext.onEditorAction(EditorInfo.IME_ACTION_DONE)
    
  • 1

    对于活动,

    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    

    对于片段,请使用 getActivity()

    . getActivity()getSystemService();

    . getActivity()getCurrentFocus();

    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    
  • 6

    这个解决方案对我来说很完美:

    private void showKeyboard(EditText editText) {
        editText.requestFocus();
        editText.setFocusableInTouchMode(true);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
        editText.setSelection(editText.getText().length());
    }
    
    private void closeKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
    
  • 8

    试试这个...

    • 用于显示键盘
    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
    • 隐藏键盘
    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
  • 8

    您可以在按钮单击事件中使用此代码

    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    
  • 0

    Crash Null Point异常修复:我遇到了一个用户点击按钮时键盘可能无法打开的情况 . 您必须编写if语句来检查getCurrentFocus()不是null:

    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if(getCurrentFocus() != null) {
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    
  • 27

    科特林的例子:

    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    

    来自片段:

    inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    

    来自活动:

    inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    
  • 11

    如果设置 android:singleLine="true" ,按钮会自动隐藏键盘

相关问题