Android - EditText made programmatically not showing keyboard

岁酱吖の 提交于 2021-02-09 20:23:07

问题


I am making an app where I am using a ArrayAdapter<String> and I am programmatically making an editText view inside of a Relative Layout which is all inside of a AppCompatDialogFragment. Each of the editTexts are showing up and I can click on them, but if it doesn't open the keyboard to type. I have tried things like:

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

Even if I try to manually open the keyboard, it still doesn't open.

        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;

回答1:


try this

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

in xml

android:focusableInTouchMode="true"



回答2:


You may try to call imm.showSoftInput [...] somewhat delayed with new Handler().postDelayed(new Runnable(){...}, 100L); since it takes time until the requestFocus() is effective, especially when your application is just starting up / your views. What you can also try is to call the requestFocus() delayed the same way.




回答3:


You can set focusable programmatically:

editText.setFocusableInTouchMode(true);



来源:https://stackoverflow.com/questions/62314661/android-edittext-made-programmatically-not-showing-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!