【问题标题】:How to set MultiLine and imeOptions="actionDone" for an EditText on Android?如何在 Android 上为 EditText 设置 MultiLine 和 imeOptions="actionDone"?
【发布时间】:2012-08-17 09:51:40
【问题描述】:

如何同时设置这些选项:

  • android:minLines="3"
  • android:inputType="textMultiLine"
  • android:imeOptions="actionDone"

好像我一输入android:inputType="textMultiLine",键盘就会自动将OK键替换为Enter键。有谁知道是否可以同时拥有两个密钥?

注意:这个answer 不是我要找的。我想要两把钥匙。

【问题讨论】:

  • 对我来说,我能够编译自己的 IME,但选项没有通过,它是 android 决定放在那里的。

标签: android android-softkeyboard


【解决方案1】:

您好,我也遇到了同样的问题,我终于找到了解决方案。

改变

android:inputType="textMultiLine"

android:inputType="text"

在 .java 文件中使用 id 访问 EditText

editText.setHorizontallyScrolling(false);

editText.setMaxLines(3);

现在在 editText 上实现 OnEditorAction。

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == 4) { //actionId 4 for actionDone And 6 for actionSend


                //perform action what you want

                return true;
            } else
                return false;
        }
    });

【讨论】:

    【解决方案2】:

    唯一可以保证的是,Android 会将 inputTypeimeOptions 传递给 IME。 IME 对它们的处理取决于实现。如果某些 IME可能决定在多行模式下有足够的屏幕空间来显示两个键,则不应依赖该行为。

    【讨论】:

      【解决方案3】:

      已在此处为类似问题写了一个答案:https://stackoverflow.com/a/42236407/7550472,事实证明这对我来说是一种可取之处,因为其他任何方法都不起作用。为了更方便访问,我也将代码粘贴到这里给像我这样的懒人;)。

      在你的 Java 代码中:

      ////////////Code to Hide SoftKeyboard on Enter (DONE) Press///////////////
      editText.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
      editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
      editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
      
      editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
          {
                      if (event == null) {
                          if (actionId == EditorInfo.IME_ACTION_DONE) {
                              // Capture soft enters in a singleLine EditText that is the last EditText
                              // This one is useful for the new list case, when there are no existing ListItems
                              EditText.clearFocus();
                              InputMethodManager inputMethodManager = (InputMethodManager)  getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
                              inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                          }
      
                          else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                              // Capture soft enters in other singleLine EditTexts
                          } else if (actionId == EditorInfo.IME_ACTION_GO) {
                          } else {
                              // Let the system handle all other null KeyEvents
                              return false;
                          }
                      } 
              else if (actionId == EditorInfo.IME_NULL) {
                          // Capture most soft enters in multi-line EditTexts and all hard enters;
                          // They supply a zero actionId and a valid keyEvent rather than
                          // a non-zero actionId and a null event like the previous cases.
                          if (event.getAction() == KeyEvent.ACTION_DOWN) {
                              // We capture the event when the key is first pressed.
                          } else {
                              // We consume the event when the key is released.
                              return true;
                          }
                      } 
              else {
                          // We let the system handle it when the listener is triggered by something that
                          // wasn't an enter.
                          return false;
                      }
                      return true;
              }
      });
      

      XML 中定义的minLines 将保持不变,而其他两个属性则不需要,因为它在 Java 代码中处理。

      【讨论】:

        【解决方案4】:

        如果你创建一个 EditText 的子类并插入这个函数,它应该可以解决你的问题。

        这个问题在https://stackoverflow.com/a/5037488/7403656 得到了回答,但是我做了一点改动,从 xml 中获取了 imeOption,而不是仅仅将其设置为 Done 选项。

        @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            InputConnection connection = super.onCreateInputConnection(outAttrs);
            int imeOptions = getImeOptions();
            int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
            if ((imeActions & imeOptions) != 0) {
                // clear the existing action
                outAttrs.imeOptions ^= imeActions;
                // set the DONE action
                outAttrs.imeOptions |= imeOptions;
            }
            if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
                outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
            }
            return connection;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-07-14
          • 1970-01-01
          • 2015-01-16
          • 2017-07-17
          • 1970-01-01
          • 2014-05-27
          • 2016-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多