当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: