【问题标题】:How to prevent the popup of keyboard for editText while activity is loaded如何防止在加载活动时弹出editText的键盘
【发布时间】:2020-12-30 02:02:01
【问题描述】:

我有一个调查活动,其中包括单选按钮、复选框、文本框等选项类型。我正在使用 EditText 来获取用户对文本框的响应。我正在从回收器视图适配器动态地膨胀 EditText。 但是每当调用此活动时,键盘也会随着光标在 EditText 框中弹出。但我只希望键盘仅在用户触摸 EditText 时打开。我尝试了几种可能的方法,但没有奏效,需要您的帮助。提前致谢!!

fun showTextBox(answerLL : LinearLayout, currentItem : QuestionList, position: Int, draftOptions : MutableList<Draft>) {
    answerLL.removeAllViews()
    var optMaster : OptionMaster
    answerLL.setPadding(0, 0, 55, 0)
    val et = EditText(mActivity)
    et.setMinLines(1);
    et.setMaxLines(10);
    et.setCursorVisible(true);
    et.requestFocusFromTouch();
 //   et.requestFocus()
    et.setFocusableInTouchMode(true)
    val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    et.setLayoutParams(lp);
    et.setTextIsSelectable(true);
    et.setGravity(Gravity.START);
    et.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
    et.setPaddingRelative(15, 7, 7, 7);
    et.setPadding(15, 7, 7, 7);
    et.setTextColor(Color.BLACK);
    et.addTextChangedListener(object: TextWatcher{
        override fun afterTextChanged(s: Editable?) {

                textListener.textValueEntered(currentItem.questionMaster.qmID, s.toString())

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }
    })
    for(draft in draftOptions){
        if (draft.quesId == currentItem.questionMaster.qmID){
            et.setText(draft.optValue)
        }
    }
    answerLL.addView(et)
    }

【问题讨论】:

  • 制作第一个(垂直第一个)TextBox 或任何其他非EditText focusable。您的编辑文本可能是布局中第一个可聚焦的元素,这就是键盘打开的原因。或者......深入挖掘并找出它打开的原因。
  • 但默认情况下,第一个可聚焦元素获得焦点,如果它最终成为 EditText,则键盘将显示。简单的解决方案是使其上方(或之前)的其他内容也可聚焦。更难的解决方案是区分是否应该打开键盘以及何时打开键盘,并以编程方式关闭键盘直到它应该打开。或者更改编辑文本的可聚焦性,直到它准备好接受输入(例如,当活动完成加载时)
  • @vishnu jm 在添加编辑文本时,您已将编辑文本的焦点设置为 false ,只有这样才能达到您的需要,et.setFocusableInTouchMode(false); et.setFocusable(false);

标签: android android-activity android-edittext android-softkeyboard


【解决方案1】:

检查清单文件中的windowSoftInputModeattribute,活动标签

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2023-03-15
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多