问题
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