创建活动时,我试图显示键盘。但是未显示键盘。

我正在使用Nexus_5_API_22_x86模拟器

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();
}


可能是什么问题?

我还尝试了在启动时未显示键盘的方法:

@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);

}

最佳答案

我做了一个例子,看到了这一点,对我有用的那条线就是打开

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


然后关闭

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


编辑:

我在三星Galaxy S4(真实设备)中尝试了我的示例

在模拟器中,键盘不会显示

10-04 10:13
查看更多