【问题标题】:EditText disapears when clicked after fragment transaction [duplicate]片段事务后单击时EditText消失[重复]
【发布时间】:2020-02-20 02:44:08
【问题描述】:

我的 EditText 工作得非常好,直到您打开另一个片段并返回到第一个片段。单击 EditText 后,键盘弹出,看起来软键盘将片段的内容向上推到背景图像后面。

这看起来像这样:

在你打开一个片段并再次关闭它之后:

我的搜索文本视图:

<EditText
                android:id="@+id/search_view"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"
                android:background="@drawable/textfield"
                android:digits="@string/digits"
                android:drawableStart="@drawable/ic_search"
                android:drawablePadding="10dp"
                android:fontFamily="@font/opensans_normal"
                android:hint="@string/search"
                android:inputType="text"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:singleLine="true"
                android:textColor="@color/colorFont"
                android:textColorHint="@color/colorFont"
                android:textSize="15sp" />

当这个方法被调用时,错误发生:

  fun addFilter() {
    val topicList = mutableListOf<Topic?>()
    topicAdapter = TopicAdapter(topicList, context, null, true)

    if (CachedData.filter.isNotEmpty()) {

        if (CachedData.filter[0] != null) {
            filterList.visibility = View.VISIBLE
            topicList.add(Topic(CachedData.filter[0], selected = true, clickable = true))
        }

        if (CachedData.filter[1] != null) {
            filterList.visibility = View.VISIBLE
            if (CachedData.filter[0] == null) {
                topicAdapter.setSubselect(-1)
            }
            topicList.add(Topic(CachedData.filter[1], selected = true, clickable = true))
        }
    }
    filterList.adapter = topicAdapter
    filterList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}

更新

这只发生在第一个 Fragment 中的某些内容发生更改时。例如,如果我在第一个片段中从 RecylerView 向数据集添加数据,则会发生错误

【问题讨论】:

  • 分享您的代码,以便人们更好地了解问题。
  • 你确定这不会在打开第二个片段之前发生吗?

标签: android kotlin android-edittext


【解决方案1】:

打开第二个片段时隐藏软键盘。使用此代码:

public void hideKeyboard(View view) {
        if (view != null) {
            view.clearFocus();
            InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

并在第一个片段的onPause() 处调用hideKeyboard() 方法,如下所示:

@Override
    public void onPause() {
        super.onPause();
        if (yourEdittext!= null)
            hideKeyboard(yourEdittext);
    }

windowSoftInputMode 到清单中的活动,如下所示:

        <activity
            android:name=".YourActivity"
            android:windowSoftInputMode="adjustResize|stateAlwaysHidden|adjustPan">
        </activity>

【讨论】:

    【解决方案2】:

    只需在您的活动中覆盖方法。它也将自动在其子片段中工作......

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (currentFocus != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
        }
        return super.dispatchTouchEvent(ev)
    }
    

    还可以在您的活动、片段中使用以下实用程序函数来隐藏软键盘。

    fun Fragment.hideKeyboard() {
    view?.let { activity?.hideKeyboard(it) }
    }
    
    fun Activity.hideKeyboard() {
    hideKeyboard(currentFocus ?: View(this))
    }
    
    fun Context.hideKeyboard(view: View) {
    val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }
    

    无论您在对话框片段和/或活动等中的代码如何,这都会关闭键盘。

    在Activity/Fragment中的使用:

    hideKeyboard()
    

    【讨论】:

      【解决方案3】:

      manifest.xml 文件中为对应的活动设置android:windowSoftInputMode="adjustPan|adjustResize"

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2017-03-23
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        相关资源
        最近更新 更多