简体   繁体   中英

Keyboard not shown even with editText.requestFocus

I am trying to show keyboard when activity is created. But the keyboard is not shown.

I am using Nexus_5_API_22_x86 emulator

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ex);
    EditText editText = (EditText) findViewById(R.id.ex);
    InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    editText.requestFocus();
}

What could be the issue?

I also tried which did not show keyboard at startup time:

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first

    TimerTask tt = new TimerTask() {

        @Override
        public void run() {
            EditText editText = (EditText) findViewById(R.id.ex);
            editText.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        }
    };

    final Timer timer = new Timer();
    timer.schedule(tt, 200);

}

I did and example to see this and the line that worked for me was this to open

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

and this to close

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Edit :

I tried my example in a Samsung Galaxy S4 (real device)

In the emulator the keyboard won't show

Have you tried to set requestFocus() first like this

editText.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

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