Implicit “Submit” after hitting Done on the keyboard at the last EditText

前端 未结 10 589
无人及你
无人及你 2020-11-29 17:51

I\'ve used some apps where when I fill my username, then go to my password, if I hit \"Done\" on the keyboard, the login form is automatically submitted, without me having t

相关标签:
10条回答
  • 2020-11-29 18:13

    Just extend 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)
    }
    
    0 讨论(0)
  • 2020-11-29 18:19

    You need to set the IME Options on your EditText.

    <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" />
    

    Then add a OnEditorActionListener to the view to listen for the "done" action.

    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;
        }
    });
    

    Official API doc: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent

    0 讨论(0)
  • 2020-11-29 18:24
     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;
         }
     });
    
    0 讨论(0)
  • 2020-11-29 18:27

    Try this:

    In your layout put/edit this:

    <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" />
    

    In your activity put this (e. g. in 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;
         }
     });
    

    Where submit_btn is your submit button with your onclick handler attached.

    0 讨论(0)
  • 2020-11-29 18:31
    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
    
    0 讨论(0)
  • 2020-11-29 18:32
    <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" />
    

    in the java file

    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;
        }
    });
    
    0 讨论(0)
提交回复
热议问题