【问题标题】:Implicit "Submit" after hitting Done on the keyboard at the last EditText在最后一个 EditText 的键盘上点击 Done 后隐式“提交”
【发布时间】:2013-10-07 05:23:34
【问题描述】:

我使用过一些应用程序,当我填写我的用户名,然后转到我的密码时,如果我在键盘上点击“完成”,登录表单会自动提交,而无需我点击提交按钮。这是怎么做到的?

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

在你的布局中放置/编辑这个:

<EditText
    android:id="@+id/search_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:singleLine="true"
    android:imeOptions="actionDone" />

在你的活动中放这个(例如在 onCreate 中):

 // your text box
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

submit_btn 是附有 onclick 处理程序的提交按钮。

【讨论】:

  • submit_btn.performClick(); 灼伤了我的眼睛。呸呸呸?为什么不调用提交方法?
  • @LaurentMeyer 在这些情况下模拟用户输入通常比直接调用底层逻辑要好。例如,提交按钮当前可能被禁用,因此 performClick() 不会执行任何操作(如预期的那样),但如果您直接调用提交方法,则必须首先检查该按钮是否未被禁用。它还会播放“点击”的声音,就像点击按钮一样。
  • @LaurentMeyer UI 敏感是什么意思?过去 6 个月内有 5 人,当然。给他们时间,人们可能也会同意我的看法。 ;)
  • 假设您更改了 UI,将按钮用于其他用途。代码将是一团糟,更糟糕的是,您需要有非常广泛的测试程序来检测这种错误。更糟糕的是,当您使用这种做法共享 UI 组件时。
  • TWIMC,在我的 EditText 中使用 imeActionLabel 禁用了所有这些行为。小心
【解决方案2】:

您需要在 EditText 上设置 IME 选项。

<EditText
    android:id="@+id/some_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Whatever"
    android:inputType="text"
    android:imeOptions="actionDone" />

然后在视图中添加OnEditorActionListener 以监听“完成”操作。

EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // TODO do something
            handled = true;
        }
        return handled;
    }
});

官方API文档:https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent

【讨论】:

    【解决方案3】:

    使用 Kotlin 的简单有效的解决方案

    扩展EditText:

    fun EditText.onSubmit(func: () -> Unit) {
        setOnEditorActionListener { _, actionId, _ ->
    
           if (actionId == EditorInfo.IME_ACTION_DONE) {
               func()
           }
    
           true
    
        }
    }
    

    然后像这样使用新方法:

    editText.onSubmit { submit() }
    

    submit() 是这样的:

    fun submit() {
        // call to api
    }
    

    更通用的扩展

    fun EditText.on(actionId: Int, func: () -> Unit) {
        setOnEditorActionListener { _, receivedActionId, _ ->
    
           if (actionId == receivedActionId) {
               func()
           }
    
            true
        }
    }
    

    然后你可以用它来监听你的事件:

    email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
    

    【讨论】:

      【解决方案4】:

      这是怎么做的

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

      别忘了添加关注

      <EditText android:layout_height="wrap_content"
      
          android:layout_width="wrap_content"
      
          android:imeOptions="actionDone"/>
      

      actionDone 在您的 EditText 中。

      【讨论】:

        【解决方案5】:

        在你的edittext标签内的XML文件中添加下面的sn-p

        android:imeOptions="actionDone"
        

        然后在您的 Java 类中,编写以下代码

        editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
            @Override 
            public boolean onEditorAction(TextView v, int id, KeyEvent event) { 
                if (id == EditorInfo.IME_ACTION_DONE) { 
                    //do something here 
                    return true;
                }
                return false; 
            } 
        });
        

        【讨论】:

          【解决方案6】:

          在edittext中添加以下行

          android:imeOptions="actionDone"
          

          愉快的编码

          【讨论】:

            【解决方案7】:
            etParola = (EditText) findViewById(R.id.etParola); 
             btnGiris = (Button) findViewById(R.id.btnGiris);
              etParola.setOnEditorActionListener(new EditText.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_DONE) {
                                btnGiris.performClick();
                                return true;
                            }
                            return false;
                        }
                    });
            
             and;
            
            
            layout xml etParola
            android:imeOptions="actionDone" add
            

            【讨论】:

            • 这与this one 的答案完全相同。您应该解释一下您认为这如何解决 OP 的问题。
            【解决方案8】:

            只需扩展this answer

            fun EditText.onSubmit(func: () -> Unit) {
                setOnEditorActionListener { _, actionId, _ ->
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        clearFocus() // if needed 
                        hideKeyboard()
                        func()
                    }
                    true
                }
            }
            
            fun EditText.hideKeyboard() {
                val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(this.windowToken, 0)
            }
            

            【讨论】:

              【解决方案9】:
               EditText edit_txt = (EditText) findViewById(R.id.search_edit);
              
               edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
                   @Override
                   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
              // which is u had set a imeoption
                       if (actionId == EditorInfo.IME_ACTION_DONE) {
                           submit_btn.performClick();
                           return true;
                       }
                       return false;
                   }
               });
              

              【讨论】:

                【解决方案10】:
                <EditText
                    android:id="@+id/signinscr_userName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/userName"
                    android:imeOptions="actionNext" />
                
                <EditText
                    android:id="@+id/signinscr_password"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/password"
                    android:imeOptions="actionDone"
                    android:inputType="textPassword" />
                

                在java文件中

                EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
                EditText passwordField = (EditText) findViewById(R.id.signinscr_password);
                
                passwordField.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                        //Do your operation here.
                        return false;
                    }
                });
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-11-13
                  • 2019-09-10
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-03-25
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-03-08
                  相关资源
                  最近更新 更多