Android 按下按钮时关闭虚拟键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3400028/
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
Close virtual keyboard on button press
提问by Andrew
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.
我有一个Activity
带EditText
,一个按钮和一个ListView
。目的是在 中键入搜索屏幕EditText
,按 按钮并使搜索结果填充此列表。
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.
如果我点击EditText
,我会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击我的搜索按钮,则虚拟键盘会保留并且我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。
Thanks for your help
谢谢你的帮助
采纳答案by Andrew
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;
}
});
回答by Paul Maserrat
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.
我把这个放在onClick(View v)
活动结束后。
You need to import android.view.inputmethod.InputMethodManager
;
您需要导入android.view.inputmethod.InputMethodManager
;
The keyboard hides when you click the button.
单击按钮时键盘会隐藏。
回答by Prashant Maheshwari Andro
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) {
}
}
});
回答by pixel
You should implement OnEditorActionListener
for your EditView
你应该OnEditorActionListener
为你的 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
您还应该使用以下方法触发隐藏在按钮中的键盘 onClickListener
Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.
现在单击虚拟键盘和按钮上的“完成”将执行相同的操作 - 隐藏键盘并执行单击操作。
回答by Ashana.Hymanol
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);
回答by Laert
Since you have only one edit text then just call action done for that edit text inside your button click and the rest is handled by the system. If you had more than one edittext then this would not be so efficent because you have to get the focused edittext first. But in your case it will work perfectly
由于您只有一个编辑文本,因此只需在您的按钮单击中调用该编辑文本完成的操作,其余部分由系统处理。如果您有多个编辑文本,那么这将不会那么有效,因为您必须首先获得焦点编辑文本。但在你的情况下它会完美地工作
myedittext.onEditorAction(EditorInfo.IME_ACTION_DONE)
回答by Priya Rajan
For Activity,
对于活动,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
For Fragments, use getActivity()
对于片段,使用getActivity()
getActivity().getSystemService();
getActivity().getSystemService();
getActivity().getCurrentFocus();
getActivity().getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
回答by Giedrius ?likas
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);
}
回答by Affa Musaffa
Try this...
尝试这个...
For Showing keyboard
editText.requestFocus(); InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
For Hide keyboard
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
用于显示键盘
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);
回答by Farruh Habibullaev
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}