【问题标题】:Multiline EditText with Done SoftInput Action Label on 2.32.3 上带有完成 SoftInput 操作标签的多行 EditText
【发布时间】:2011-06-28 04:55:25
【问题描述】:

有没有办法让多行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

这是:

  • class= TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL
  • 标志 = InputType.TYPE_TEXT_FLAG_MULTI_LINE

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    在您的 XML 中使用这些属性。

    android:inputType="textImeMultiLine"

    android:imeOptions="actionDone"

    【讨论】:

      【解决方案2】:

      要完成操作,您可以使用:

      XML

      android:inputType="text|textCapSentences"    
      

      JAVA

      editText.setHorizontallyScrolling(false);
      editText.setMaxLines(Integer.MAX_VALUE);
      

      我希望它对你有用。

      【讨论】:

        【解决方案3】:

        按照上一个答案

        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"/> 
        

        【讨论】:

          【解决方案4】:

          对 EditText 类进行子类化的另一种解决方案是使用以下内容配置您的 EditText 实例:

          editText.setHorizontallyScrolling(false);
          editText.setMaxLines(Integer.MAX_VALUE);
          

          至少,这适用于我在 Android 4.0 上。它配置 EditText 实例,以便用户编辑在多行上显示的单行字符串,即使设置了 IME 操作也是如此。

          【讨论】:

          • @Futzilogik :先生,您应该得到更多的支持!这个答案是一个救生员,也是一个简单的答案。我的意思是,天哪,哇。我希望我可以多次投票。非常感谢!
          • 我正在运行 4.4.2,这对我不起作用,或者至少对我的方式不起作用。我只用 XML 做的,所以这可能是个问题。我将 inputType 配置为“textEmailAddress|textMultiLine”,将 scrollHorizo​​ntally 配置为 false,将 maxLines 配置为 500(xml 文件),将 singleLine 配置为 false,并将 imeOptions 配置为 actionNext。我尝试从输入类型中删除“textMultiLine”。使用 textMultiLine,我得到键盘上的 ENTER 键,没有它,所有内容都保持在一行上并且仍然水平滚动。这看起来确实是最简单的解决方案,但上面的那个对我有用,所以现在就用它吧。
          • 关于我之前的评论(无法编辑),怀疑是没有设置 MAX_VALUE 的问题,或者只是在创建编辑文本后更改了这些,我尝试了此处所示的代码,并且它有效!我只是想为别人发帖,你不能用 XML 来做(或者我反正做不到)。我的其他设置:singleLine=false、imeOptions=actionNext、inputType=textEmailAddress(没有多行)。
          • 效果很好。这是改进旧答案的一个很好的例子!
          • 在运行 Lollipop 的 Galaxy S5 上为我工作。很好的解决方案。奇怪的是在xml中设置水平滚动效果不一样
          【解决方案5】:

          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)会使此功能无效!我建议你从inputType="text" 开始。以下是在 XML 中使用它的方法。

          <com.example.views.ActionEditText
              android:id=...
              android:layout_stuff=...
              android:imeOptions="actionDone"
              android:inputType="textAutoCorrect|textCapSentences|textMultiLine"
              android:maxLines="3" />
          

          【讨论】:

          • 如果您的代码适用于 2.3,我的也应该如此。它们几乎相同,我基于我的或你的代码,所以谢谢! 2.3 中是否存在 4.0 中没有的特性?标准的“对多行编辑不执行任何操作”是 Google 添加的故意行为。
          • 我在 2.3(和 4.x)上试过这个,它适用于我的应用程序。
          • @matej.smitala 是的,你不能两者兼得。
          • 经过数小时寻找最佳解决方案后,这似乎是无需回车键即可实现多行编辑文本的最佳且最简单的方法。
          • 九年后,这仍然像 SDK 30 的魅力一样有效?
          【解决方案6】:

          显然,原始问题的答案是肯定的,但我相信 Android 团队正试图让开发人员稍微思考一下他们​​如何使用多行 EditText。他们希望 Enter 键添加换行符,并且可能希望您提供一个按钮或其他输入方式来引发您已完成编辑的事件。

          我有同样的问题,我的明显解决方案是简单地添加一个完成按钮并让输入按钮添加换行符。

          【讨论】:

          • 抱歉,我的问题可能不够清楚。多行用于软包装单行输入。即不允许使用换行符。
          • @Mullins:您是否在 SoftInput 键盘中添加了自己的“完成”按钮?您是如何在保持“Enter”的同时做到这一点的?
          • 不。我在 UI 上创建了一个与键盘分开的完成按钮。
          • 好吧,也许 Android 团队应该遵循他们自己的建议,让消息应用程序中的多行 TextEdit 的操作按钮创建换行符,而不是发送消息。
          【解决方案7】:

          好吧,在重新阅读 TextViewEditorInfo 文档后,很明显该平台将强制 IME_FLAG_NO_ENTER_ACTION 用于多行文本视图。

          注意TextView 会自动 在多行上为您设置此标志 文本视图。

          我的解决方案是继承 EditText 并在让平台配置它们后调整 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,尽管这可以通过繁琐的布局配置来实现。

          【讨论】:

          • 我通常不会给出像“天哪,谢谢”这样的通用 cmets,但这个答案很有帮助,而且我认为它值得认可。总之,天哪,谢谢。 :-)
          • +1 作为答案,但如果您在这种情况下从代码中设置编辑文本的输入类型。它删除垂直滚动并添加水平滚动。要解决该问题,请使用以下代码。 editTextObj.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE);如果您通过在多个布局中包含相同的视图来重用视图,通常会发生这种情况。 @ohhorob 非常感谢您的解决方案。
          • 它就像一个魅力!但我真的不明白代码。你有一些地方可以让我了解整个标志机制吗?
          • 谢谢,我是初学者,我把它放在使用对话框EditText 的活动中,所以它不在我的活动xml 中。我尝试将此代码放入我的活动中,但出现错误:The method onCreateInputConnection(EditorInfo) of type SMSMain must override or implement a supertype method。一开始你打电话给super,所以不确定是什么问题。你有什么建议吗?谢谢。
          • @NoniA。您必须创建 EditText 的子类。在那个类中你必须调用这个方法。创建一个延伸到 EditText 的类。并将该方法放入该类中。
          猜你喜欢
          • 2011-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-08
          • 2011-11-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多