【问题标题】:Android: how to make keyboard enter button say "Search" and handle its click?Android:如何让键盘输入按钮说“搜索”并处理它的点击?
【发布时间】:2011-03-13 10:20:37
【问题描述】:

我想不通。一些应用程序有一个 EditText(文本框),当您触摸它并调出屏幕键盘时,该键盘有一个“搜索”按钮而不是回车键。

我想实现这个。如何实现该搜索按钮并检测搜索按钮的按下?

编辑:找到了如何实现搜索按钮;在 XML 中,android:imeOptions="actionSearch" 或在 Java 中,EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);。但是如何处理用户按下该搜索按钮?和android:imeActionId有关系吗?

【问题讨论】:

  • 请注意,imeOptions 可能无法在某些设备上运行。见thisthis

标签: android android-softkeyboard


【解决方案1】:

在布局中将输入法选项设置为搜索。

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

在java中添加编辑器动作监听器。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

【讨论】:

  • 在 os 2.3.6 上它在我放置 android:inputType="text" 属性之前不起作用。
  • android:inputType="text" 在 Android 2.3.5 和 4.0.4 上我也需要
  • @Carol EditTextTextView 的子类。
  • android:inputType="text" 对于 4.4.0 - 4.4.2 (Android Kitkat) 也是必需的。
  • 是的,在 5.0 中仍然需要 android:inputType="text" :)
【解决方案2】:

当用户点击搜索时隐藏键盘。除了 Robby Pond 答案

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    //...perform search
}

【讨论】:

    【解决方案3】:

    xml文件中,放入imeOptions="actionSearch"inputType="text"maxLines="1"

    <EditText
        android:id="@+id/search_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1" />
    

    【讨论】:

      【解决方案4】:

      在科特林中

      evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
          if (actionId == EditorInfo.IME_ACTION_DONE) {
              doTheLoginWork()
          }
          true
      }
      

      部分 XML 代码

       <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
             <android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
      
                  android:layout_marginBottom="8dp"
                  android:layout_marginTop="8dp"
                  android:paddingLeft="24dp"
                  android:paddingRight="24dp">
      
                  <EditText
                      android:id="@+id/evLoginUserEmail"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/email"
                      android:inputType="textEmailAddress"
                      android:textColor="@color/black_54_percent" />
              </android.support.design.widget.TextInputLayout>
      
              <android.support.design.widget.TextInputLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="8dp"
                  android:layout_marginTop="8dp"
                  android:paddingLeft="24dp"
                  android:paddingRight="24dp">
      
                  <EditText
                      android:id="@+id/evLoginPassword"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/password"
                      android:inputType="textPassword"
                      android:imeOptions="actionDone"
                      android:textColor="@color/black_54_percent" />
              </android.support.design.widget.TextInputLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        通过 XML:

         <EditText
                android:id="@+id/search_edit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/search"
                android:imeOptions="actionSearch"
                android:inputType="text" />
        

        通过 Java:

         editText.clearFocus();
            InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
        

        【讨论】:

          【解决方案6】:

          此答案适用于 TextInputEditText :

          在布局 XML 文件中,将输入法选项设置为所需的类型。例如完成

          <com.google.android.material.textfield.TextInputLayout
                  android:id="@+id/textInputLayout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
          
                  <com.google.android.material.textfield.TextInputEditText
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:imeOptions="actionGo"/>
          

          同样,也可以将imeOptions设置为actionSubmit、actionSearch等

          在java中添加编辑器动作监听器。

          TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);
          
          textInputLayout.getEditText().setOnEditorActionListener(new 
          
              TextView.OnEditorActionListener() {
                  @Override
                  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                      if (actionId == EditorInfo.IME_ACTION_GO) {
                          performYourAction();
                          return true;
                      }
                      return false;
                  }
              });
          

          如果您使用的是 kotlin:

          textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
              if (actionId == EditorInfo.IME_ACTION_GO) {
                  performYourAction()
              }
              true
          }
          

          【讨论】:

          • 对于 Android/Kotlin 新手来说,知道这个 sn-p 应该去哪里以及它应该在什么上下文中会很有帮助...
          • 如果你是一个新手,我建议将这些代码 sn-ps 放入你的活动或片段的 onCreate 方法或 onCreateView 方法中。
          • 谢谢。我找到了答案并将 onCreate() 放入活动中。我的问题的另一部分与上下文有关,因为“testInputLayout”不清楚。我的解决方案是: findViewById( R.id.search_request_layout ).editText?.setOnEditorActionListener { _, actionId, _ -> // 做点什么 }
          猜你喜欢
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 2013-07-27
          • 1970-01-01
          • 1970-01-01
          • 2013-01-24
          • 1970-01-01
          • 2019-01-21
          相关资源
          最近更新 更多