Android: softkeyboard not showing up

2019-02-25 01:59发布

I have 2 EditTexts in the MainActivity Layout. If i run the application normally the 1st EditText gets focused but the softkeyboard is not openned.

but when i used this:

public class TestingActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText et1 = (EditText) findViewById(R.id.editText1);
        EditText et2 = (EditText) findViewById(R.id.editText2);

        et2.requestFocus();
        InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
    }
}

expecting the 2nd EditText will get focus and softkeyboard will be openned.

I only get focus, but the softkeyboard is openned only when i click on the EditText.

Thank You

5条回答
【Aperson】
2楼-- · 2019-02-25 02:23

For getting the focus to particular edittext just add the tag inside your edit text.

<EditText 
    android:id="@+id/etBox"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="enter into editbox"
    >
    <requestFocus/>
    </EditText>
查看更多
Bombasti
3楼-- · 2019-02-25 02:37
    et2.clearFocus();
    et2.requestFocus();
    InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);

I meet the problem on Android N platform and resolve it by refocusing the editview. I don`t know the real reason why the editview should be cleared first,but it works fine for me.

查看更多
贪生不怕死
4楼-- · 2019-02-25 02:37

Sometimes you will need to post-delay showing keyboard command, so in my case, i did the following

editText.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        }
    }, 300);
查看更多
孤傲高冷的网名
5楼-- · 2019-02-25 02:41

Try specifying the android:windowSoftInputMode attribute in your AndroidManifest.xml file for your activity.

For example:

<activity android:name=".TestingActivity" android:windowSoftInputMode="stateVisible|adjustResize" />

You probably don't need any of the code that uses InputMethodManager in your Activity.

查看更多
叼着烟拽天下
6楼-- · 2019-02-25 02:46

I notice that one reason for the keyboard not showing up is selecting an inputtype not supported by the specific Android device. For instance InputType.TYPE_NUMBER_VARIATION_NORMAL will not work on my Asus Transformer (no keyboard shows up), while InputType.TYPE_CLASS_NUMBER will work just fine.

查看更多
登录 后发表回答