如何制作 Android EditView“完成”按钮并在单击时隐藏键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1919742/
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
How do I make an Android EditView 'Done' button and hide the keyboard when clicked?
提问by d-man
When the user clicks on the EditView
, Android opens the keyboard so that user can write in the EditView
.
当用户点击 时EditView
,Android 会打开键盘,以便用户可以在EditView
.
The problem is, when the user is done writing, there is no way to hide the keyboard. The user has to press the back button to hide the keyboard.
问题是,当用户写完后,没有办法隐藏键盘。用户必须按下后退按钮才能隐藏键盘。
Is there a way to display a Done
button on the keyboard that will hide the keyboard?
有没有办法Done
在键盘上显示一个隐藏键盘的按钮?
回答by Paresh Mayani
First you need to set the android:imeOptions
attribute equal to actionDone
for your target EditText as seen below. That will change your ‘RETURN' button in your EditText's soft keyboard to a ‘DONE' button.
首先,您需要为目标 EditText设置android:imeOptions
属性等于,actionDone
如下所示。这会将 EditText 软键盘中的“返回”按钮更改为“完成”按钮。
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:imeOptions="actionDone"
/>
回答by dmazzoni
Use TextView.setImeOptionsand pass it actionDone.
like textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
使用TextView.setImeOptions并将其传递给 actionDone。喜欢textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
回答by Oded Breiner
Include bothimeOptions
andsingleLine
:
包括两个imeOptions
和singleLine
:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
回答by Santosh Pillai
android:imeActionLabel="Done"
android:singleLine="true"
In the XML file works just fine. But this will also cause the editText
to keep typing in a single line which you may not want. So adding following to your code will make sure that you won't end up typing everything on a single line.
在 XML 文件中工作得很好。但这也会导致editText
继续输入您可能不想要的一行。因此,在您的代码中添加以下内容将确保您最终不会在一行中输入所有内容。
mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");
回答by Shangeeth Sivan
For getting the done Button
为了完成按钮
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
and
和
android:inputType="text"
in the xml
android:inputType="text"
在xml中
For handling on done clicked from keyboard
用于处理从键盘点击完成
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if(actionId == EditorInfo.IME_ACTION_DONE){
// Your action on done
return true;
}
return false;
}
});
`
`
回答by Frank Nguyen
Use this:
用这个:
android:singleLine="true"
回答by NarenderNishad
Use These two lines to your EditText
将这两行用于您的 EditText
android:imeActionLabel="Done"
android:singleLine="true"
or you can achieve it Programmatically by this line.
或者您可以通过此行以编程方式实现它。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
回答by Tom
If the property does not change for the widget it may be better to use like
android:imeOptions="actionDone"
in the layout xml
file.
如果小部件的属性没有改变,最好
android:imeOptions="actionDone"
在布局xml
文件中使用。
回答by Kiran Mahale
Use:
用:
android:imeActionLabel="Done"
android:singleLine="true"
回答by Steven.Nguyen
For the code:
对于代码:
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);