android set hidden the keyboard on press enter (in a EditText)

前端 未结 5 1157
醉酒成梦
醉酒成梦 2021-01-31 04:20

When my user press Enter on the virtual android \"user validate entry!\" keyboard my keyboard stay visible! (Why?)

Here my Java code...

privat         


        
相关标签:
5条回答
  • 2021-01-31 04:29

    Keep the singleLine="true" and add imeOptions="actionDone" to the EditText. Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):

    if (actionId == EditorInfo.IME_ACTION_DONE) {
    
                    if ((username.getText().toString().length() > 0)
                            && (password.getText().toString().length() > 0)) {
                        // Perform action on key press
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(username.getWindowToken(),
                                0);
                        doLogin();
                    }
                }
    
    0 讨论(0)
  • 2021-01-31 04:39

    I am create a custom component who extends AutoCompleteTextView, like in the example below:

    public class PortugueseCompleteTextView extends AutoCompleteTextView {
    ...
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
            InputMethodManager inputManager =
                    (InputMethodManager) getContext().
                            getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(
                    this.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
        return super.onKeyPreIme(keyCode, event);
    }
    

    I am using this code in the AlertDialog.Builder, but is possible to be using to Activity.

    0 讨论(0)
  • 2021-01-31 04:42

    This should do it:

    yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {
    
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
                    // NOTE: In the author's example, he uses an identifier
                    // called searchBar. If setting this code on your EditText
                    // then use v.getWindowToken() as a reference to your 
                    // EditText is passed into this callback as a TextView
    
                    in.hideSoftInputFromWindow(searchBar
                            .getApplicationWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                   userValidateEntry();
                   // Must return true here to consume event
                   return true;
    
                }
                return false;
            }
        });
    
    0 讨论(0)
  • 2021-01-31 04:50

    just add this line in your edit text.

    android:imeOptions="actionDone"'
    

    you can specify the next edit text id to move to that edit text on click of the keyboard done button.

    0 讨论(0)
  • 2021-01-31 04:51

    If you make the text box a single line (I believe the propery is called SingleLine in the layout xml files) it will exit out of the keyboard on enter.

    Here you go: http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine

    0 讨论(0)
提交回复
热议问题