2.3 上带有 Done SoftInput 操作标签的多行 EditText
有没有办法在 Android 2.3 上显示多行 EditText
并使用 IME 操作标签“完成”?
在 Android 2.2 中,这不是问题,回车按钮显示 IME 操作标签“完成”(android:imeActionLabel="actionDone"
),并在单击时关闭软输入。
将 EditText
配置为多行时,Android 2.3 删除了显示软输入键盘“完成”操作的功能。
我已设法使用 KeyListener 更改软输入输入按钮的行为,但输入按钮仍然看起来像输入键。
这是 EditText
的声明
<EditText
android:id="@+id/Comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:lines="3"
android:maxLines="3"
android:minLines="3"
android:maxLength="60"
android:scrollHorizontally="false"
android:hint="hint"
android:gravity="top|left"
android:textColor="#888"
android:textSize="14dp"
/>
<!-- android:inputType="text" will kill the multiline on 2.3! -->
<!-- android:imeOptions="actionDone" switches to a "t9" like soft input -->
当我在活动中加载设置内容视图后检查 inputType
值时,它显示为:
inputType = 0x20001
这是:
- class =
类型类文本 | TYPE_TEXT_VARIATION_NORMAL
- 标志 =
InputType.TYPE_TEXT_FLAG_MULTI_LINE
Is there a way to have a Multi-Line EditText
present and use the IME Action Label "Done" on Android 2.3?
In Android 2.2 this is not a problem, the enter button shows the IME Action Label "Done" (android:imeActionLabel="actionDone"
), and dismisses Soft Input when clicked.
When configuring an EditText
for multi-line, Android 2.3 removes the ability to show the "Done" action for the Soft Input keyboard.
I have managed to alter the behaviour of the Soft Input enter button by using a KeyListener
, however the enter button still looks like an enter key.
Here is the declaration of the EditText
<EditText
android:id="@+id/Comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:lines="3"
android:maxLines="3"
android:minLines="3"
android:maxLength="60"
android:scrollHorizontally="false"
android:hint="hint"
android:gravity="top|left"
android:textColor="#888"
android:textSize="14dp"
/>
<!-- android:inputType="text" will kill the multiline on 2.3! -->
<!-- android:imeOptions="actionDone" switches to a "t9" like soft input -->
When I check the inputType
value after loading setting the content view in the activity, it shows up as:
inputType = 0x20001
Which is:
- class =
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL
- flags =
InputType.TYPE_TEXT_FLAG_MULTI_LINE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,在重新阅读
TextView
和EditorInfo
文档之后,很明显该平台将强制IME_FLAG_NO_ENTER_ACTION
用于多行文本视图。我的解决方案是继承
EditText
并在让平台配置 IME 选项后调整它们:在上面,我也强制
IME_ACTION_DONE
,即使这可以通过繁琐的布局配置。Well, after re-reading the
TextView
andEditorInfo
docs, it has become clear that the platform is going to forceIME_FLAG_NO_ENTER_ACTION
for multi-line text views.My solution is to subclass
EditText
and adjust the IME options after letting the platform configure them:In the above, I'm forcing
IME_ACTION_DONE
too, even though that can be achieved through tedious layout configuration.Ohhorob的答案基本上是正确的,但是他的代码真的非常多余!它基本上相当于这个更简单的版本(为懒惰的读者提供的完整代码):
请注意,某些
inputType
选项(例如textShortMessage
)使此不起作用!我建议您从inputType="text"
开始。以下是如何在 XML 中使用它。Ohhorob's answer is basically correct, but his code is really really redundant! It is basically equivalent to this much simpler version (full code for lazy readers):
Note that some
inputType
options such astextShortMessage
make this not work! I suggest you start withinputType="text"
. Here is how you could use it in your XML.子类化 EditText 类的另一种解决方案是使用以下内容配置 EditText 实例:
至少,这对我在 Android 4.0 上有效。它配置 EditText 实例,以便用户编辑在多行上软换行显示的单行字符串,即使设置了 IME 操作也是如此。
An alternative solution to subclassing the EditText class is to configure your EditText instance with this:
At least, this works for me on Android 4.0. It configures the EditText instance so that the user edits a single-line string that is displayed with soft-wrapping on multiple lines, even if an IME action is set.
按照之前的答案
使用这个就像
Following previous answer
Use this like
为了完成操作,您可以使用:
XML
JAVA
我希望它对您有用。
for put the action Done, you could use:
XML
JAVA
I hope its work for you.
显然最初问题的答案是肯定的,但我相信 Android 团队正在努力让开发人员思考如何使用多行 EditText。他们希望使用 Enter 键来添加换行符,并且可能希望您提供一个按钮或其他输入方式来引发您已完成编辑的事件。
我有同样的问题,我的明显解决方案只是添加一个完成按钮并让输入按钮添加换行符。
Apparently the answer to the original question is Yes but I believe the Android team are trying to make developers think a little bit about how they use the multi-line EditText. They want the enter key to add newlines and probably expect that you provide a button or another input means to raise the event that you are done editing.
I have the same issue and my obvious solution was simply to add a done button and let the enter button add the newlines.
在 XML 中使用这些属性。
Use these attribute in your XML.