当EditText以编程方式聚焦时,我有一个小功能来打开软键盘,如下所示......

public void getUserName() {
    EditText tv = (EditText)findViewById(R.id.user_info_name);
    tv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                showDialog("Focused!");
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            }
        }
    });
    tv.selectAll();
    tv.requestFocus();
}

但是,软键盘不会自动出现,但对话框显示说明焦点 . 为了让键盘出现,我必须在EditText里面单击 .

我的XML如下......

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@id/user_info_name"
        android:editable="true"
        android:hint="@string/user_info_name"
        android:inputType="textCapWords|textPersonName"
        android:textColor="@color/blue_gray"
        android:maxLength="50"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:enabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true" />

有人可以告诉你为什么它不工作或我缺少什么/没有解决 .

一如既往地感谢您 .

SOLVED: The following change to the function fixed the issue...

public void getUserName() {
    EditText tv = (EditText)findViewById(R.id.user_info_name);
    tv.selectAll();
    tv.requestFocus();
    InputMethodManager imm = (InputMethodManager)this.getSystemService(this.INPUT_METHOD_SERVICE);
    imm.showSoftInput(tv,InputMethodManager.SHOW_IMPLICIT);
}