【问题标题】:imeOptions "actionNext" programmatically - how to jump to next field?imeOptions“actionNext”以编程方式 - 如何跳转到下一个字段?
【发布时间】:2011-03-28 09:56:05
【问题描述】:

在布局 XML 中,可以指定 android:imeOptions="actionNext" 在虚拟键盘中添加 Next 按钮,然后单击它 - 焦点会跳转到下一个字段。

如何以编程方式执行此操作 - 例如基于某些事件触发焦点转到下一个字段?

【问题讨论】:

    标签: android textview ime imeoptions


    【解决方案1】:

    搜索下一个可聚焦字段,然后调用 requestFocus()

    TextView nextField = (TextView)currentField.focusSearch(View.FOCUS_RIGHT);
    nextField.requestFocus();
    

    【讨论】:

    • 这不是关于这个问题的标题的真实答案,但它在技术上是一个有效的答案。下面的答案更合适。
    • @SeanGlover 你不应该使用像 below 这样的词,因为答案的位置会不断变化。
    【解决方案2】:

    您可以将 EditorInfo 类中的常量用于 IME 选项。 喜欢,

    editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
    

    【讨论】:

    • 是的,否则它会插入一个新行并跳转到那个 =)
    • 这如何回答 OP 的问题?这不会自动跳转到下一个字段。
    【解决方案3】:

    除了虚拟 QWERTY 键盘中可用的默认键之外,始终需要添加额外的键。

    使用 XML

    <EditText android:text="@+id/EditText01" 
    android:id="@+id/EditText01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:imeOptions="actionDone"/>
    

    By Programmatic Way

    当您必须在 Android 应用程序中处理任何类型的用户输入时,EditorInfo 是最有用的类。

    IME_ACTION_DONE:此操作执行“完成”操作,无需输入任何内容,IME 将关闭。

     EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);
    

    欲了解更多信息,您可以访问http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html

    【讨论】:

      【解决方案4】:

      只是建议,如果你正在使用

           EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE); 
      

      它不起作用,请确保您的 EditText 使用的是单行。

      例如:

             editTextSample.setSingleLine();
      

      【讨论】:

      • 谢谢。这应该是公认的答案。只需在 setImeOptions 之前调用 setSingleLine()。
      【解决方案5】:

      科特林吊坠

      editText.imeOptions = EditorInfo.IME_ACTION_DONE
      

      【讨论】:

        【解决方案6】:
        editText.setLines(1);
        editText.setSingleLine(true);
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);
        

        我解决了这个问题,确保单行,点击进入时进入下一个editText

        【讨论】:

          【解决方案7】:

          在我的情况下,设置一个 imeOptions 来解决问题。

          edtAnswer.maxLines = 1
          edtAnswer.inputType = InputType.TYPE_CLASS_TEXT
          edtAnswer.imeOptions = EditorInfo.IME_ACTION_NEXT
          

          【讨论】:

            【解决方案8】:

            你可以这样做

            edittext.imeOptions = EditorInfo.IME_ACTION_DONE //for done button
            

            edittext.imeOptions = EditorInfo.IME_ACTION_NEXT //for next button
            

            但是…… 您需要了解,如果您对 edittext 使用过滤器,那么您需要设置

            edittext.setSingleLine()
            

            【讨论】:

              【解决方案9】:

              您可以使用以下代码跳转到下一个字段:

              BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
              inputConnection.performEditorAction(EditorInfo.IME_ACTION_NEXT);
              //Use EditorInfo.IME_ACTION_UNSPECIFIED if you set android:imeOptions on the EditText
              

              【讨论】:

                【解决方案10】:

                我尝试了所有答案,但 EditorAction 对我有用!

                EditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)

                我的 XML 布局:

                  <EditText
                            android:id="@+id/input1"
                            style="@style/edittext"
                            android:nextFocusForward="@id/input2"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"/>
                        <EditText
                            android:id="@+id/input2"
                            style="@style/edittext"
                            android:nextFocusLeft="@id/input1"
                            android:layout_weight="1"
                            android:nextFocusRight="@id/input3"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"/>
                

                和 Kotlin 代码:

                input1.onEditorAction(EditorInfo.IME_ACTION_NEXT)
                

                现在焦点移到input2 Edittext。

                【讨论】:

                  猜你喜欢
                  • 2014-06-11
                  • 2021-10-03
                  • 2012-10-11
                  • 2019-10-29
                  • 1970-01-01
                  • 2023-03-30
                  • 2013-02-18
                  • 2011-09-15
                  • 2014-05-27
                  相关资源
                  最近更新 更多