Android 按下回车键时隐藏软键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26645212/
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
Hide Soft keyboard on return key press
提问by cph2117
I've searched half a dozen other answers on SO, but haven't found one that works. All I'm trying to do is dismiss the soft keyboard when the user presses the enter button. (The equivalent of the insanely easy iOS 'resignKeyboard' call.) In the following code, the onEditorAction method does not get called. I've set up an EditText view in my XML file and the code in my fragment is as follows:
我在 SO 上搜索了六个其他答案,但没有找到一个有效的。我要做的就是在用户按下输入按钮时关闭软键盘。(相当于极其简单的 iOS 'resignKeyboard' 调用。)在下面的代码中,没有调用 onEditorAction 方法。我在 XML 文件中设置了 EditText 视图,片段中的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);
textField = (EditText) fragView.findViewById(R.id.textField);
textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId,
KeyEvent arg2) {
// hide the keyboard and search the web when the enter key
// button is pressed
if (actionId == EditorInfo.IME_ACTION_GO
|| actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_SEARCH
|| (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);
return true;
}
return false;
}
});
// Inflate the layout for this fragment
return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);
}
The following is a snippet from the XML file where I define the EditText field. I need EditText to be multiline.
以下是我在其中定义 EditText 字段的 XML 文件的片段。我需要 EditText 是多行的。
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/Encrypted_Text_Hint"
android:gravity="top"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>
回答by George Thomas
If you dont want multiple line,for you edittext you can just specify single line for the edittext and also you can put imeOptions as Done like this
如果您不想要多行,对于您的 edittext,您可以只为 edittext 指定单行,也可以像这样将 imeOptions 设置为 Done
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
I clearly dunno whether you are trying to achieve this or not.Any way take a look.
我显然不知道您是否正在尝试实现这一目标。无论如何都可以看看。
EDIT:android:singleLine is deprecated since API 3 due to bad performance, you have to use android:maxLines instead. singleLine will be available because even now some effects are not supported by android:maxLines attribute.
编辑:由于性能不佳,android:singleLine 已被弃用,因为 API 3,您必须改用 android:maxLines。singleLine 将可用,因为即使现在 android:maxLines 属性也不支持某些效果。
回答by cph2117
I solved the issue by changing the following in the XML file from:
我通过更改 XML 文件中的以下内容解决了这个问题:
android:inputType="textMultiLine"
to:
到:
android:inputType="textImeMultiLine"
回答by Ketan Patel
Below code works for me.
下面的代码对我有用。
IN XML:
在 XML 中:
android:imeOptions="actionDone"
android:inputType="textCapWords"
IN JAVA CLASS:
在 Java 类中:
edit_text.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if ((actionId==EditorInfo.IME_ACTION_DONE ) )
{
//Toast.makeText(getActivity(), "call",45).show();
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//or try following:
//InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
return true;
}
return false;
}
});
回答by Chizoba Ogbonna
Based on my tests, what is really required to dismiss the soft keyboard, when a user clicks the enter button, can be achieved simply by adding the code below to your EditText in xml.
根据我的测试,当用户单击 Enter 按钮时,关闭软键盘真正需要的是什么,只需将下面的代码添加到 xml 中的 EditText 即可。
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
No need for OnEditorActionListener or any other Java code.
不需要 OnEditorActionListener 或任何其他 Java 代码。
回答by Jorge Chavez
Accepted answer is deprecated:
已接受的答案已弃用:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
Try with this:
试试这个:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:maxLines="1"
/>
回答by VipPunkJoshers Droopy
EditText.xml
编辑文本文件
<EditText
android:id="@+id/edit_text_searh_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:singleLine="true"
android:lines="1"
android:hint="Ingresa palabras claves"
android:imeActionLabel="Search"
android:background="@drawable/rounded_edit_text_search"
android:drawableRight="@android:drawable/ic_menu_search"/>
Fragment.java
片段.java
final EditText edit_text_searh = (EditText) v.findViewById(R.id.edit_text_searh_home);
edit_text_searh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_text_searh.getWindowToken(), 0);
Toast.makeText(getContext(),edit_text_searh.getText(),Toast.LENGTH_SHORT).show();
return true;
}
});
回答by Kevin Bloom
This is what worked for me. Based on this layout xml:
这对我有用。基于此布局xml:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ip_override_text_input_sat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:hint="IP Address" />
</com.google.android.material.textfield.TextInputLayout>
I needed to do this for the keyboard to dismiss and the text into to lose focus and hide the cursor.
我需要这样做才能让键盘关闭并输入文本以失去焦点并隐藏光标。
findViewById<TextView>(R.id.ip_override_text_input_sat).setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ENTER && currentFocus != null) {
val inputManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(
this.currentFocus!!.windowToken,
HIDE_NOT_ALWAYS
)
currentFocus!!.clearFocus()
return@setOnKeyListener true
}
return@setOnKeyListener false
}
Hope this helps someone doing the same
希望这有助于有人做同样的事情
回答by VVB
This may be you could add a attribute to your EditText like this:
这可能是您可以像这样向 EditText 添加一个属性:
android:imeOptions="actionSearch"
回答by samsad
Try this method, It may be solve your problem.
试试这个方法,或许能解决你的问题。
protected void showKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() == null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
} else {
View view = activity.getCurrentFocus();
inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
}
}
/**
* Hide keyboard.
*/
protected void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
if (inputMethodManager.isAcceptingText())
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
} else {
if (view instanceof EditText)
((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}