在2.3上使用完成的SoftInput操作标签多行EditText
有没有办法让多行EditText目前在Android 2.3上使用IME操作标签“完成”? 
 在Android 2.2中这不是问题,inputbutton显示IME操作标签“完成”( android:imeActionLabel="actionDone" ),并单击时android:imeActionLabel="actionDone"软input。 
 当为多行configurationEditText ,Android 2.3将无法显示软input键盘的“完成”操作。 
 我已经设法通过使用KeyListener来改变软inputinputbutton的行为,但是inputbutton仍然看起来像一个回车键。 
 这是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_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMALTYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL
-   flags = InputType.TYPE_TEXT_FLAG_MULTI_LINE
 那么,在重新阅读TextView和EditorInfo文档之后,很明显的是平台将强制IME_FLAG_NO_ENTER_ACTION用于多行文本视图。 
请注意,
TextView将在多行文本视图中为您自动设置此标志。
 我的解决scheme是EditText然后在让平台configuration它们之后调整IME选项: 
 @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection connection = super.onCreateInputConnection(outAttrs); int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION; if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) { // clear the existing action outAttrs.imeOptions ^= imeActions; // set the DONE action outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; } if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; } return connection; } 
 在上面,我也强制IME_ACTION_DONE ,即使这可以通过繁琐的布局configuration来实现。 
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 { public ActionEditText(Context context) { super(context); } public ActionEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ActionEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection conn = super.onCreateInputConnection(outAttrs); outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; return conn; } } 
 请注意,一些inputType选项,如textShortMessage使这不工作! 我build议你从inputType="text" 。 这里是你如何在你的XML中使用它。 
 <com.example.views.ActionEditText android:id=... android:layout_stuff=... android:imeOptions="actionDone" android:inputType="textAutoCorrect|textCapSentences|textMultiLine" android:maxLines="3" /> 
inheritanceEditText类的另一种解决scheme是使用以下方法configurationEditText实例:
 editText.setHorizontallyScrolling(false); editText.setMaxLines(Integer.MAX_VALUE); 
至less,这在Android 4.0上适用于我。 它configurationEditText实例,以便用户编辑单行string,即使设置了IME操作,也可以在多行上进行软包装显示。
以前的答案
 public class MultiLineText extends EditText { public MultiLineText(Context context) { super(context); } public MultiLineText(Context context, AttributeSet attrs) { super(context, attrs); } public MultiLineText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection connection = super.onCreateInputConnection(outAttrs); int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION; if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) { // clear the existing action outAttrs.imeOptions ^= imeActions; // set the DONE action outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE; } if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; } return connection; } } 
像这样使用
 <myapp.commun.MultiLineText android:id="@+id/textNotes" android:layout_height="wrap_content" android:minHeight="100dp" android:layout_width="wrap_content" android:hint="Notes" android:textSize="20sp" android:padding="7dp" android:maxLines="4"/> 
为了把行动完成,你可以使用:
XML
 android:inputType="text|textCapSentences" 
JAVA
 editText.setHorizontallyScrolling(false); editText.setMaxLines(Integer.MAX_VALUE); 
我希望它为你工作。
显然原始问题的答案是肯定的,但我相信Android团队正在试图让开发人员考虑一下他们如何使用多行EditText。 他们希望回车键添加换行符,并且可能期望您提供一个button或其他input方法来引发您正在编辑的事件。
我有同样的问题,我明显的解决scheme只是添加一个完成button,让inputbutton添加换行符。
在你的XML中使用这些属性。
安卓的inputType = “textImeMultiLine”
机器人:imeOptions = “actionDone”