在2.3上完成SoftInput操作标签的多行EditText

有没有办法EditText在Android 2.3上使用多行显示并使用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

这是:


类= TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL

标志= InputType.TYPE_TEXT_FLAG_MULTI_LINE


守着星空守着你
浏览 501回答 3
3回答

慕的地8271018

好了,在重新阅读TextView和EditorInfo文档之后,很明显该平台将强制IME_FLAG_NO_ENTER_ACTION使用多行文本视图。请注意,TextView它将在多行文本视图上自动为您设置此标志。我的解决方案是EditText在平台配置它们之后,继承并调整IME选项:@Overridepublic InputConnection onCreateInputConnection(EditorInfo outAttrs) {&nbsp; &nbsp; InputConnection connection = super.onCreateInputConnection(outAttrs);&nbsp; &nbsp; int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;&nbsp; &nbsp; if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; // clear the existing action&nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions ^= imeActions;&nbsp; &nbsp; &nbsp; &nbsp; // set the DONE action&nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;&nbsp; &nbsp; }&nbsp; &nbsp; if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;&nbsp; &nbsp; }&nbsp; &nbsp; return connection;}在上面,IME_ACTION_DONE即使可以通过乏味的布局配置来实现,我也要强制执行。

偶然的你

Ohhorob的答案基本上是正确的,但是他的代码确实非常多余!它基本上等效于这个简单得多的版本(适合懒惰读者的完整代码):package com.example.views;import android.content.Context;import android.util.AttributeSet;import android.view.inputmethod.EditorInfo;import android.view.inputmethod.InputConnection;import android.widget.EditText;// An EditText that lets you use actions ("Done", "Go", etc.) on multi-line edits.public class ActionEditText extends EditText{&nbsp; &nbsp; public ActionEditText(Context context)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public ActionEditText(Context context, AttributeSet attrs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public ActionEditText(Context context, AttributeSet attrs, int defStyle)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public InputConnection onCreateInputConnection(EditorInfo outAttrs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InputConnection conn = super.onCreateInputConnection(outAttrs);&nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;&nbsp; &nbsp; &nbsp; &nbsp; return conn;&nbsp; &nbsp; }}请注意,某些inputType选项(例如,textShortMessage使其无法使用)!我建议你从开始inputType="text"。这是在XML中使用它的方式。<com.example.views.ActionEditText&nbsp; &nbsp; android:id=...&nbsp; &nbsp; android:layout_stuff=...&nbsp; &nbsp; android:imeOptions="actionDone"&nbsp; &nbsp; android:inputType="textAutoCorrect|textCapSentences|textMultiLine"&nbsp; &nbsp; android:maxLines="3" />

繁星淼淼

遵循先前的答案public class MultiLineText extends EditText {&nbsp; &nbsp; public MultiLineText(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public MultiLineText(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public MultiLineText(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public InputConnection onCreateInputConnection(EditorInfo outAttrs) {&nbsp; &nbsp; &nbsp; &nbsp; InputConnection connection = super.onCreateInputConnection(outAttrs);&nbsp; &nbsp; &nbsp; &nbsp; int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;&nbsp; &nbsp; &nbsp; &nbsp; if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // clear the existing action&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions ^= imeActions;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set the DONE action&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return connection;&nbsp; &nbsp; }}像这样使用<myapp.commun.MultiLineText&nbsp; android:id="@+id/textNotes"&nbsp; android:layout_height="wrap_content"&nbsp; android:minHeight="100dp"&nbsp; android:layout_width="wrap_content"&nbsp; android:hint="Notes"&nbsp; android:textSize="20sp"&nbsp; android:padding="7dp"&nbsp; android:maxLines="4"/>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android