简体   繁体   中英

Android soft touch keyboard

I'm new to programming with Android and am trying to figure out how to get the soft touch keyboard to appear when an EditText item is clicked in, (when the activity loads there is only one user input field, so it stays in focus) so far I have the below method I made however when it shows the keyboard, the keyboard cannot be closed by the user... the button to close the keyboard is there however it does not hide it.

public void showKeyboard(View view) {
    InputMethodManager myKeyboard = (InputMethodManager)getSystemService(Context
        .INPUT_METHOD_SERVICE);
    myKeyboard.showSoftInput(PN_input,InputMethodManager.SHOW_IMPLICIT);
}

Any help or point in the right direction would be helpful :)

Edit: Hide keyboard method I'm using when the find button is clicked

public void hideKeyboard() {
    InputMethodManager myKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    myKeyboard.hideSoftInputFromWindow(PN_input.getWindowToken(),0);
}

Put this to your Activity onCreate to hide softkey when Activity starts below code hides your softkey.

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);// for hide keypad

When you touch on EditText, automatically softkey will appears, No need of Code for do this, for hiding softkey touch anywhere inside the screen.

For Show KeyPad use this code :

 InputMethodManager imm = (InputMethodManager)
                                 getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }

For Hide Keypad use this code :

 InputMethodManager imm = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

From digging in the android developer website, I was able to create a method that works to show the keyboard and the 'done' button works to close the keyboard.

    InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    myKeyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM