Android Use Done button on Keyboard to click button

后端 未结 13 1262
-上瘾入骨i
-上瘾入骨i 2020-12-02 06:30

Ok in my app I have a field for the user to input a number. I have the field set to only accept numbers. When the user clicks on the field it brings up the keyboard. On the

相关标签:
13条回答
  • 2020-12-02 06:44

    Try this:

    max.setOnKeyListener(new OnKeyListener(){
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event){
            if(keyCode == event.KEYCODE_ENTER){
                //do what you want
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-02 06:47

    if you want to catch the keyboard enter button for doing your job which you want to done through any event like button click, you can write the below simple code for that text view

    Edittext ed= (EditText) findViewById(R.id.edit_text);
    
    ed.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // Do you job here which you want to done through event
        }
        return false;
    }
    });
    
    0 讨论(0)
  • 2020-12-02 06:48

    Your Last Edittext .setOnEditorActionListener call this method automatic hit api

    I was Call in LoginActivity in et_password

     et_Pass.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
    
                        Log.i(TAG,"Enter pressed");
                        Log.i(Check Internet," and Connect To Server");
    
                    }
                    return false;
                }
            });
    

    Working Fine

    0 讨论(0)
  • 2020-12-02 06:56

    You can try with IME_ACTION_DONE .

    This action performs a “done” operation for nothing to input and the IME will be closed.

     Your_EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    boolean handled = false;
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                      /* Write your logic here that will be executed when user taps next button */
    
    
                        handled = true;
                    }
                    return handled;
                }
            });
    
    0 讨论(0)
  • 2020-12-02 06:58

    And this is a Kotlin version:

    editText.setOnEditorActionListener { v, actionId, event ->
      if(actionId == EditorInfo.IME_ACTION_DONE){
          //Put your action there
          true
      } else {
          false
      }
    }
    
    0 讨论(0)
  • 2020-12-02 07:00
    max.setOnKeyListener(new OnKeyListener(){
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event){
        if(keyCode == event.KEYCODE_ENTER){
            //do what you want
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题