简体   繁体   中英

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 . 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;

try this

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

in xml

android:focusableInTouchMode="true"

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.

You can set focusable programmatically:

editText.setFocusableInTouchMode(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM