【问题标题】:Android - EditText made programmatically not showing keyboardAndroid - EditText 以编程方式不显示键盘
【发布时间】:2020-09-30 12:32:59
【问题描述】:

我正在制作一个使用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;

【问题讨论】:

    标签: android android-studio android-layout android-fragments android-appcompat


    【解决方案1】:

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

    editText.setFocusableInTouchMode(true);

    【讨论】:

      【解决方案2】:

      试试这个

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

      在xml中

      android:focusableInTouchMode="true"
      

      【讨论】:

      • 非常感谢,s.mhmd 的回答!我不能把它放在 xml 中,因为我只从代码创建这个编辑文本。还能用吗?
      • 尝试在清单文件中添加 >> android:windowSoftInputMode="stateAlwaysVisible" ->。
      【解决方案3】:

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

      【讨论】:

      • 感谢 Lurzapps 的快速响应。我不太明白你上面的意思。你能解释一下吗?
      • 我曾经遇到过早调用requestFocus的问题,直到我终于意识到我必须延迟调用它
      • 哦,这更有意义!非常感谢,Lurzapps!
      猜你喜欢
      • 2013-08-23
      • 2021-01-27
      • 1970-01-01
      • 2016-06-15
      • 2012-04-04
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多