Android: show soft keyboard automatically when foc

2018-12-31 23:18发布

I'm showing an input box using AlertDialog. The EditText inside the dialog itself is automatically focused when I call AlertDialog.show(), but the soft keyboard is not automatically shown.

How do I make the soft keyboard automatically show when the dialog is shown? (and there is no physical/hardware keyboard). Similar to how when I press the Search button to invoke the global search, the soft keyboard is automatically shown.

24条回答
浅入江南
2楼-- · 2019-01-01 00:05

As horkavlna wrote,

toggle

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

and hide keyboard

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 

methods work. But show variants don't work in my case. So in onCreate() I put hideKeyboard(editText); then in onStart() I write toggleKeyboard(editText); and in onStop() I write hideKeyboard(editText);.

There are three problems:

1) when an application starts with turned off screen it will hide the keyboard,

2) every time you turn on the screen it will show the keyboard,

3) after application finish you can see the keyboard in Android main screen.

After several tests I removed these methods and in AndroidManifest in activity tags wrote android:windowSoftInputMode="stateVisible" or android:windowSoftInputMode="stateAlwaysHidden".

查看更多
泛滥B
3楼-- · 2019-01-01 00:06
<activity
    ...
    android:windowSoftInputMode="stateVisible" >
</activity>

or

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
查看更多
只靠听说
4楼-- · 2019-01-01 00:06

Well, this is a pretty old post, still there is something to add.
These are 2 simple methods that help me to keep keyboard under control and they work just perfect:

Show keyboard

public void showKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    View v = getCurrentFocus();
    if (v != null)
        imm.showSoftInput(v, 0);
}

Hide keyboard

public void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    View v = getCurrentFocus();
    if (v != null)
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
查看更多
刘海飞了
5楼-- · 2019-01-01 00:06

Yes you can do with setOnFocusChangeListener it will help you.

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});
查看更多
临风纵饮
6楼-- · 2019-01-01 00:07

Try this

SomeUtils.java

public static void showKeyboard(Activity activity, boolean show) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

    if(show)
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    else
        inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
}
查看更多
无色无味的生活
7楼-- · 2019-01-01 00:08

try and use:

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
查看更多
登录 后发表回答