Android EditText:完成而不是 Enter 或自动换行而不是多行
我有一个多行 EditText,不允许行返回。现在,一旦单击“保存”,我就会用一些空格替换回车符。有什么方法可以用“完成”按钮替换屏幕上的输入按钮? (就像单行 EditText 一样)
我知道我仍然应该删除回车符 (\r\n|\r|\n
) 因为屏幕键盘不是添加的唯一方法他们。
这是我当前的 XML
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minLines="3" android:gravity="left|top"
android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
android:imeOptions="actionDone" />
I have a multiple line EditText that does not permit line returns. Right now I am replacing returns with some spaces as soon as they click save. Is there any way I can replace the on screen enter button with a Done button? (like it is for single line EditText)
I am aware that I should still strip out returns (\r\n|\r|\n
) because the on screen keyboard is not the only way to add them.
Here is my current XML
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minLines="3" android:gravity="left|top"
android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
android:imeOptions="actionDone" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议阅读这篇文章
http://savagelook.com/blog/android/android-quick-tip-edittext-with-done-button-that-closes-the-keyboard
非常好的例子
I suggest to read this article
http://savagelook.com/blog/android/android-quick-tip-edittext-with-done-button-that-closes-the-keyboard
really good example
您需要将输入类型设置为电子邮件地址或电子邮件主题。任何一种都会给您带来您想要的结果。
shouldAdvanceFocusOnEnter()
是 TextView 中的私有方法,用于确定是否输入新行或将焦点移至下一个字段。You need to set the input type as email address or email subject. Either one will give you your desired result.
shouldAdvanceFocusOnEnter()
is a private method in TextView which determines whether to enter a new line or move focus to next field.如果您在 XML 中使用
android:inputType="textMultiLine|..."
或使用相应的 Java 代码:那么这是显示 ✔︎ 完成 的唯一解决方案或
If you're using
android:inputType="textMultiLine|..."
in your XML, or using the corresponding Java code:then the only solution to show a ✔︎ Done or ???? Search button is to follow the answers here:
Multiline EditText with Done SoftInput Action Label on 2.3
So you should extend
EditText
and overrideonCreateInputConnection()
to manually set the IME_ACTION_xx flags; something like this...This is because whenever you enable the
"textMultiLine"
option, it ignores any setting ofandroid:imeOptions="actionDone"
orandroid:imeActionLabel="actionDone"
, which is very strange and confusing.我对带有 actionLabel 的多行文本执行此操作:
I do this for multiline texts with an actionLabel: