【问题标题】:Use "ENTER" key on softkeyboard instead of clicking button使用软键盘上的“ENTER”键而不是单击按钮
【发布时间】:2011-05-25 23:33:35
【问题描述】:

你好 我搜索了EditText 并搜索了Button。当我输入搜索到的文本时,我想使用软键盘上的 ENTER 键而不是搜索Button 来激活搜索功能。

提前感谢您的帮助。

【问题讨论】:

    标签: android keyboard android-softkeyboard


    【解决方案1】:

    您可以通过在 EditText 上设置 OnKeyListener 来实现。

    这是我自己的代码示例。我有一个名为addCourseTextEditText,当点击回车键或方向键时,它将调用函数addCourseFromTextBox

    addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
    addCourseText.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        addCourseFromTextBox();
                        return true;
                    default:
                        break;
                }
            }
            return false;
        }
    });
    

    【讨论】:

    • 其实对于软键是不保证的。例如,它不适用于 Nexus 7 (Android 4.2) 上的“ENTER”和“BACK”。
    • @Ghedeon 您可以为编辑文本设置android:inputType="text" xml,以显示回车键与带有回车的默认键盘。
    • 此方法不保证在 Jellybean 中有效,请参阅 developer.android.com/reference/android/view/KeyEvent.html
    • @Ghedeon 那么如何让它在 Nexus 7 上为“ENTER”工作?
    • 这个解决方案在许多设备上完全被破坏,包括 Nexus 7。不要使用它!
    【解决方案2】:
    <EditText
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_hint"
        android:inputType="text"
        android:imeOptions="actionSend" />
    

    然后,您可以通过为 EditText 元素定义 TextView.OnEditorActionListener 来监听操作按钮上的按下。在您的侦听器中,响应 EditorInfo 类中定义的相应 IME 操作 ID,例如 IME_ACTION_SEND。例如:

    EditText editText = (EditText) findViewById(R.id.search);
    editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendMessage();
                handled = true;
            }
            return handled;
        }
    });
    

    来源:https://developer.android.com/training/keyboard-input/style.html

    【讨论】:

      【解决方案3】:

      也许你可以像这样向你的 EditText 添加一个属性:

      android:imeOptions="actionSearch"
      

      【讨论】:

      • 如果您正在构建搜索编辑文本,这是更简单的解决方案。
      • 还要设置android:inputType="text"
      【解决方案4】:

      向 EditText 添加一个属性,例如 android:imeOptions="actionSearch"

      这是实现该功能的最佳方式

      而且 imeOptions 还有一些其他的值,例如 "go" 、"next"、"done" 等。

      【讨论】:

        【解决方案5】:

        我们也可以使用 Kotlin lambda

        editText.setOnKeyListener { _, keyCode, keyEvent ->
                if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    Log.d("Android view component", "Enter button was pressed")
                    return@setOnKeyListener true
                }
                return@setOnKeyListener false
            }
        

        【讨论】:

          【解决方案6】:

          实现此目的的最新方法是:

          将此添加到 XML 中的 EditText:

          android:imeOptions="actionSearch"
          

          然后在您的 Activity/Fragment 中:

          EditText.setOnEditorActionListener { _, actionId, _ ->
              if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                  // Do what you want here
                  return@setOnEditorActionListener true
              }
              return@setOnEditorActionListener false
          }
          

          【讨论】:

            【解决方案7】:

            这是我如何处理我的一个应用程序的示例

             //searching for the Edit Text in the view    
                final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
                    myEditText.setOnKeyListener(new View.OnKeyListener() {
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                             if (event.getAction() == KeyEvent.ACTION_DOWN)
                                  if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                                         (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                            //do something
                                            //true because you handle the event
                                            return true;
                                           }
                                   return false;
                                   }
                    });
            

            【讨论】:

              【解决方案8】:

              为避免焦点前进到下一个可编辑字段(如果有的话),您可能希望忽略按键事件,但处理按键事件。我也更喜欢先过滤 keyCode,假设它会稍微更有效率。顺便说一句,请记住,返回 true 意味着您已经处理了事件,因此其他侦听器不会。不管怎样,这是我的版本。

              ETFind.setOnKeyListener(new OnKeyListener()
              {
                  public boolean onKey(View v, int keyCode, KeyEvent event)
                  {
                      if (keyCode ==  KeyEvent.KEYCODE_DPAD_CENTER
                      || keyCode ==  KeyEvent.KEYCODE_ENTER) {
              
                          if (event.getAction() == KeyEvent.ACTION_DOWN) {
                              // do nothing yet
                          } else if (event.getAction() == KeyEvent.ACTION_UP) {
                                      findForward();      
                          } // is there any other option here?...
              
                          // Regardless of what we did above,
                          // we do not want to propagate the Enter key up
                          // since it was our task to handle it.
                          return true;
              
                      } else {
                          // it is not an Enter key - let others handle the event
                          return false;
                      }
                  }
              
              });
              

              【讨论】:

                猜你喜欢
                • 2015-02-21
                • 1970-01-01
                • 2014-02-25
                • 1970-01-01
                • 2013-01-22
                • 2023-03-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多