【问题标题】:How do I make an Android EditView 'Done' button and hide the keyboard when clicked?如何制作 Android EditView“完成”按钮并在单击时隐藏键盘?
【发布时间】:2010-12-27 13:04:52
【问题描述】:

当用户点击EditView时,Android会打开键盘以便用户在EditView中书写。

问题是,当用户写完后,没有办法隐藏键盘。用户必须按后退按钮才能隐藏键盘。

有没有办法在键盘上显示Done 按钮来隐藏键盘?

【问题讨论】:

标签: android keyboard


【解决方案1】:

首先,您需要为您的目标 EditText 设置 android:imeOptions 属性等于 actionDone,如下所示。这会将 EditText 软键盘中的“返回”按钮更改为“完成”按钮。

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionDone"
    android:singleLine="true"
    />

【讨论】:

  • @Michael 删除了该链接。谢谢
  • 我必须添加 android:singleLine="true" 才能通过 xml 工作
  • android:singleLine 已弃用。使用 android:maxLines="1"。
  • 是的 @FariborZ singleLine 现在已弃用。
  • 重要提示:singleLine 与 maxLines 不同。这种误解给每个人带来了很多问题,我们需要注意无处不在。 stackoverflow.com/questions/30879471/…
【解决方案2】:

使用TextView.setImeOptions 并将其传递给actionDone。 喜欢textView.setImeOptions(EditorInfo.IME_ACTION_DONE);

【讨论】:

  • textView.setImeOptions(EditorInfo.IME_ACTION_DONE);
  • 我必须添加 textView.singleLine(true) 才能让它以编程方式工作。
  • 但自 android 2.3 起已弃用
【解决方案3】:

包括both imeOptions singleLine

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

【讨论】:

    【解决方案4】:
    android:imeActionLabel="Done" 
    android:singleLine="true"
    

    在 XML 文件中工作得很好。但这也会导致editText 继续输入您可能不想要的单行。因此,在您的代码中添加以下内容将确保您最终不会在一行中输入所有内容。

    mainText.setHorizontallyScrolling(false);
    mainText.setMaxLines("Maximum integer value that you want to provide");
    

    【讨论】:

    • 谢谢,演示时间是 12 点,时间是 11:58,你在 11:59 解决了我的问题:p
    • android:singleLine = "true" 已被弃用。所以使用, android:lines="1"
    【解决方案5】:

    用于完成按钮

    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

    android:inputType="text" 在 xml 中

    用于处理从键盘点击完成

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
                if(actionId == EditorInfo.IME_ACTION_DONE){
                    // Your action on done
                    return true;
                }
                return false;
            }
        });
    

    `

    【讨论】:

      【解决方案6】:

      使用这个:

      android:singleLine="true"
      

      【讨论】:

      • 是的...否则如何区分进入下一行和关闭键盘?
      • 这最终为我工作。我决定将actionDone 也加入其中,以防某些设备反应不同。
      【解决方案7】:

      使用这两行到你的EditText

      android:imeActionLabel="Done"
      android:singleLine="true"
      

      或者您可以通过这一行以编程方式实现它。

      editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
      

      【讨论】:

        【解决方案8】:

        用途:

        android:imeActionLabel="Done"
        android:singleLine="true" 
        

        【讨论】:

          【解决方案9】:

          如果小部件的属性没有改变,最好使用 like android:imeOptions="actionDone" 在布局 xml 文件中。

          【讨论】:

            【解决方案10】:

            我必须指出这一点,因为很多人可能会在不知道问题的情况下陷入困境。

            如果您希望在单击 Done 时隐藏 kb,并且设置 android:imeOptions="actionDone"android:maxLines="1" 而不设置 EditText inputType,它将 EditText 的默认 inputType 不是很多人认为的 "text"

            所以,只设置inputType 会给你想要的结果whatever你设置它喜欢"text""number",...等等。

            【讨论】:

              【解决方案11】:

              Kotlin 解决方案

              在 Kotlin 中处理隐藏键盘 + 完成操作的直接方法是:

              // Set action
              edittext.setOnEditorActionListener { _, actionId, _ ->
                  if (actionId == EditorInfo.IME_ACTION_DONE) {
                      // Hide Keyboard
                      val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                      inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
                      true
                  }
                  false
              }
              

              Kotlin 扩展

              使用它在您的主代码中调用edittext.onDone {/*action*/}。使其更具可读性和可维护性

              edittext.onDone { edittext.hideKeyboard() }
              
              fun View.hideKeyboard() {
                  val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                  inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
              }
              
              fun EditText.onDone(callback: () -> Unit) {
                  // These lines optional if you don't want to set in Xml
                  imeOptions = EditorInfo.IME_ACTION_DONE
                  maxLines = 1
                  setOnEditorActionListener { _, actionId, _ ->
                      if (actionId == EditorInfo.IME_ACTION_DONE) {
                          callback.invoke()
                          true
                      }
                      false
                  }
              }
              

              其他键盘扩展

              如果您想要更多简化键盘操作的方法(显示、关闭、聚焦):Read this post

              不要忘记将这些选项添加到您的 edittext Xml(如果不在代码中)

              <EditText ...
                  android:imeOptions="actionDone"
                  android:inputType="text"/>
              

              需要inputType="textMultiLine"支持吗? Read this post 并且不要在 Xml 中添加 imeOptionsinputType

              【讨论】:

                【解决方案12】:

                代码:

                editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
                

                【讨论】:

                  【解决方案13】:

                  ActionDone 是在点击键盘上的下一个按钮时使用的 键盘是隐藏的。在编辑文本或 AppcompatEdit 中使用

                  XML

                  1.1 如果你使用 AppCompatEdittext

                      <android.support.v7.widget.AppCompatEditText
                      android:id="@+id/edittext"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:imeOptions="actionDone"/>
                  

                  1.2 如果你使用 Edittext

                      <EditText
                      android:id="@+id/edittext"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:imeOptions="actionDone"/>
                  

                  JAVA

                  EditText edittext= (EditText) findViewById(R.id.edittext);
                  edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);
                  

                  【讨论】:

                    【解决方案14】:

                    实际上,您可以为那个蓝色小按钮设置自定义文本。在 xml 文件中只需使用

                    android:imeActionLabel="whatever"
                    

                    在您的 EditText 上。

                    或者在java文件中使用

                    etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);
                    

                    我随意选择IME_ACTION_DONE作为这个函数的第二个参数应该去的例子。可以在here 找到这些操作的完整列表。

                    需要注意的是,这不会导致文本出现在所有设备的所有键盘上。某些键盘不支持该按钮上的文本(例如 swiftkey)。有些设备也不支持它。一个好的规则是,如果您看到按钮上已有文本,这会将其更改为您想要的任何内容。

                    【讨论】:

                      【解决方案15】:

                      如果你正在使用

                      android:imeOptions="actionDone" 
                      

                      那么你必须使用

                      android:inputType="text"
                      

                      那么只有您可以在键盘中看到“操作完成”按钮。

                      【讨论】:

                        【解决方案16】:

                        在你的视图中使用它

                        <EditText 
                            ....
                            ....
                            android:imeOptions="actionDone" 
                            android:id="@+id/edtName"
                            />
                        

                        【讨论】:

                          【解决方案17】:

                          如果您根本不想要任何按钮(例如,您正在为盲人开发一个无法定位且您依赖单/双/长点击的 GUI):

                          text.setItemOptions(EditorInfo.IME_ACTION_NONE)
                          

                          或者在 Kotlin 中:

                          text.imeOptions = EditorInfo.IME_ACTION_NONE
                          

                          【讨论】:

                            猜你喜欢
                            • 2012-11-15
                            • 1970-01-01
                            • 2017-10-15
                            • 2019-06-24
                            • 2017-05-23
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多