如何强制软键盘不隐藏EditText下的TextView/计数器?

7
问题在于Android键盘在打开时会覆盖EditText下的TextView(而不是EditText本身!)

以下是我的布局示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">


    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/big_image"
                android:layout_width="200dp"
                android:layout_height="600dp"
                android:layout_centerHorizontal="true"
                android:background="@color/colorPrimary"
                android:src="@drawable/ic_launcher_foreground"/>


            <android.support.design.widget.TextInputLayout
                android:id="@+id/comment_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/big_image">

                <EditText
                    android:id="@+id/comment_edittext_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="your comment"
                    android:imeOptions="actionDone"
                    android:maxLength="250"/>
            </android.support.design.widget.TextInputLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/comment_input_layout"
                android:text="0/250"/>
        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

以下图片以便更清楚地说明:

没有键盘的屏幕

实际行为

期望行为

我在stackoverflow上找到了几个相似的问题,但都没有解决方案:(请帮助我解决这个问题!

请确保我已经采用了所有关于android:windowSoftInputMode="adjustPan"android:windowSoftInputMode="adjustResize"android:focusableInTouchMode="true"android:fitsSystemWindows="true"或将此textview添加到与edittext布局分离的位置,使用framelayout作为根元素等的建议。

但是问题有点复杂。

另一个重要的事情是,这个编辑文本框下面的文本应该

  • 在文本换行时仍然位于键盘上方

  • 在编辑文本向上滚动时消失(因为它是编辑文本框的一部分)。


检查答案,你可以用稍微不同的方式实现你想要的东西,希望能对你有所帮助。 - Arbaz.in
3个回答

1
你可以使用这个设计来进行归档。
在XML中,使用 TextInputLayout 代替edittext。
                <android.support.design.widget.TextInputLayout
                    android:id="@+id/itFAuthAnswer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    app:passwordToggleEnabled="true"
                    app:counterEnabled="true"// add this 
                    app:counterMaxLength="250" //add this
                    app:hintTextAppearance="@style/AppTextInputHintLabelStyle">

                    <EditText
                        android:id="@+id/etName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/et_auth_f_que_hint"
                        android:imeOptions="actionDone"
                        android:singleLine="true"/>
                </android.support.design.widget.TextInputLayout>

这行代码以不同的方式解决了您的问题,并且也适用于您当前的设计。
app:counterEnabled="true"// make it true
app:counterMaxLength="250"//set your limit

愉快的学习...


我正在复制我的先前回答中的评论:问题不在计数器上。我只是将其添加为示例 - 系统使用计数器或下面的textview的方式相同。您可以在我的代码片段中使用此选项替换textview。问题在于键盘悬停在此textview(或您示例中的计数器)上。 - Yulia
数字 250 是从哪里来的? - IgorGanapolsky

1
我遇到了同样的问题。我已经提交了一张工单给Material询问此事。 我采取的“hack”方法是根据我的同事的建议:每当布局被渲染并且我点击具有计数器的字段(这是我的布局中的最后一个字段)时,通过编程将其滚动到底部,以便键盘不再隐藏计数器。
以下是我的程序的不同代码片段,供您参考:
    override fun onFocusChange(p0: View?, isOnFocus: Boolean) {
    if (isOnFocus.not() && getDescription().isEmpty()) {
        tnDescriptionLayout.error = resources.getString(R.string.description_error_message)
        tnDescriptionLayout.isErrorEnabled = true
    } else {
        tnDescriptionLayout.isErrorEnabled = false

        llayout.postDelayed(object : Runnable {
            override fun run() {
                var v =
                    tnDescriptionLayout.findViewById<AppCompatTextView>(R.id.textinput_counter)
                nestedScrollView.smoothScrollBy(0,v.height)
            }

        }, CoreConstants.MINIMUM_VIEW_LOAD_DURATION)
    }
    tvDescription.setText(getDescription())



    lateinit var nestedScrollView: NestedScrollView
    fun change(nestedScrollView: NestedScrollView){
      this.nestedScrollView=nestedScrollView
    }

    paymentOrderContent.change(nestedScrollView = parentView.nestedScrollViewParent)

我希望这能帮到你!


你理解了问题,太棒了,非常感谢你的回答!我得到了beta01材料,但它不起作用。尽管他们似乎已经修复了它... 关于你的回答 - 如果我写像mCommentEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {...这样的东西,然后在新的可运行的run()函数中使用你的代码 - hack只能第一次起作用,你遇到过这个问题吗? - Yulia

0
请在您的TextInputLayout中添加以下内容:app:counterEnabled="true"app:counterMaxLength="250"
<android.support.design.widget.TextInputLayout
         android:id="@+id/comment_input_layout"
         android:layout_width="match_parent"
         app:counterEnabled="true"
         app:counterMaxLength="250"
         android:layout_height="wrap_content"
         android:layout_below="@+id/big_image">
   <EditText
      android:id="@+id/comment_edittext_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="your comment"
      android:imeOptions="actionDone"
      android:maxLength="250"/> 

编辑:

请在您的清单文件中尝试使用这个。

android:windowSoftInputMode="stateVisible|adjustResize"

问题不在计数器上。我只是将其添加为示例 - 系统使用计数器或下面的文本视图工作相同。您可以在我的代码片段中使用此选项替换文本视图。问题在于键盘悬停在此文本视图(或您示例中的计数器)上 - Yulia
那你为什么不在EditText上方使用TextView计数器呢? - ismail alaoui
因为我需要这个TextView(或者在您的代码中是计数器)在EditText下方。 - Yulia
请再次检查我的问题,我肯定已经尝试了所有这些选项(正如您在问题中所读到的)。 当键盘隐藏edittext时,adjustResize很好,此时stateVisible不是必需的。我的问题是在edittext下方的counter/textview(在显示edittext或counter或textview时一切都正常,问题在于键盘只隐藏了counter而不是edittext)。 - Yulia
@ismailalaoui 你为什么认为我们需要一个 counterMaxLength - IgorGanapolsky
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接