[英]Android - EditText made programmatically not showing keyboard
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
.我正在制作一个使用ArrayAdapter<String>
的应用程序,并且我正在以编程方式在相对布局内创建一个 editText 视图,该视图全部位于AppCompatDialogFragment
内。 Each of the editTexts are showing up and I can click on them, but if it doesn't open the keyboard to type.每个editTexts都出现了,我可以点击它们,但如果它没有打开键盘输入。 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;
try this尝试这个
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
in xml在 xml
android:focusableInTouchMode="true"
You may try to call imm.showSoftInput [...]
somewhat delayed with new Handler().postDelayed(new Runnable(){...}, 100L);
您可以尝试使用new Handler().postDelayed(new Runnable(){...}, 100L);
调用imm.showSoftInput [...]
。 since it takes time until the requestFocus()
is effective, especially when your application is just starting up / your views.因为requestFocus()
生效需要时间,尤其是当您的应用程序刚刚启动/您的视图时。 What you can also try is to call the requestFocus()
delayed the same way.您还可以尝试以相同的方式调用requestFocus()
延迟。
You can set focusable programmatically:您可以以编程方式设置焦点:
editText.setFocusableInTouchMode(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.