按下按钮时关闭虚拟键盘

发布于 2024-09-12 21:06:51 字数 340 浏览 7 评论 0原文

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

这一切都工作正常,但虚拟键盘的行为很奇怪。

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

感谢您的帮助

I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.

This is all working perfectly, but the virtual keyboard is behaving strange.

If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

盛夏已如深秋| 2024-09-19 21:06:51
InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

我将其放在 onClick(View v) 事件之后。

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

单击按钮时键盘会隐藏。

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

I put this right after the onClick(View v) event.

You need to import android.view.inputmethod.InputMethodManager;

The keyboard hides when you click the button.

月亮是我掰弯的 2024-09-19 21:06:51
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;
    }
});
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;
    }
});
长途伴 2024-09-19 21:06:51

使用下面的代码

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) {

        }
    }
});

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) {

        }
    }
});
旧情勿念 2024-09-19 21:06:51

您应该为您的 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 在按钮中触发键盘隐藏

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

You should implement OnEditorActionListener for your EditView

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

And you hide keyboard by:

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

You should also fire keyboard hiding in your button using onClickListener

Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.

梦里°也失望 2024-09-19 21:06:51

对于 Activity、

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

Fragment,请使用 getActivity()

getActivity().getSystemService();

getActivity().getCurrentFocus();

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

For Activity,

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

For Fragments, use getActivity()

getActivity().getSystemService();

getActivity().getCurrentFocus();

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
夜光 2024-09-19 21:06:51

就您而言,由于您只有一个 EditText,因此我相信以下解决方案是最好的解决方案。如果您有多个 EditText 组件,那么您需要关注要关闭的 EditText,或者为每个 EditText 调用以下函数。但对您来说,这非常有效:

editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

基本上,EditText 已经具有一个函数,用于处理使用键盘后要执行的操作。我们只是传递我们想要关闭键盘。

In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:

editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.

黑寡妇 2024-09-19 21:06:51

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

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

Add the following code inside your button click event:

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
半﹌身腐败 2024-09-19 21:06:51

这个解决方案非常适合我:

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

This solution works perfect for me:

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);
}
梦一生花开无言 2024-09-19 21:06:51

试试这个...

  1. 用于显示键盘

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. 用于隐藏键盘

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    

Try this...

  1. For Showing keyboard

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. For Hide keyboard

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
梦年海沫深 2024-09-19 21:06:51
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
野の 2024-09-19 21:06:51

Kotlin 示例:

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

来自 Fragment:

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

来自 Activity:

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

Kotlin example:

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

from Fragment:

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

from Activity:

inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
蓝海 2024-09-19 21:06:51

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

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

You use this code in your button click event

// 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);
}
天暗了我发光 2024-09-19 21:06:51

崩溃空点异常修复:
我遇到过这样的情况:当用户单击按钮时键盘可能无法打开。您必须编写一个 if 语句来检查 getCurrentFocus() 是否不为空:

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

Crash Null Point Exception Fix:
I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
从﹋此江山别 2024-09-19 21:06:51

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

If you set android:singleLine="true", automatically the button hides the keyboard¡

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文