Android - 以编程方式制作的 EditText 不显示键盘

Android - EditText made programmatically not showing keyboard

我正在制作一个应用程序,我正在使用 ArrayAdapter<String> 并且我正在以编程方式在相对布局内制作一个 editText 视图,该布局全部在 AppCompatDialogFragment 内。每个 editTexts 都出现了,我可以点击它们,但如果它没有打开键盘来输入。我试过类似的东西:

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

手动打开键盘还是打不开

        RelativeLayout listLayout = new RelativeLayout(this.getContext());
        listLayout.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.WRAP_CONTENT,
                75));

        if(super.getItem(position) == ParameterTypes.String.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
            listLayout.addView(editText);
        } else if(super.getItem(position) == ParameterTypes.Integer.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            listLayout.addView(editText);
        } else if(super.getItem(position) == ParameterTypes.Double.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            listLayout.addView(editText);
        return listLayout;

您可以尝试使用 new Handler().postDelayed(new Runnable(){...}, 100L); 稍稍延迟调用 imm.showSoftInput [...],因为 requestFocus() 需要时间才能生效,尤其是当您的应用程序刚刚启动/您的视图时。您还可以尝试以相同的方式延迟调用 requestFocus()

试试这个

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

在xml

android:focusableInTouchMode="true"

您可以通过编程方式设置可聚焦:

editText.setFocusableInTouchMode(true);