簡體   English   中英

如何強制軟鍵盤不隱藏編輯文本下的文本視圖/計數器?

[英]How to force soft-keyboard NOT to hide textview/counter under edittext?

問題是android鍵盤在打開時將textview隱藏在edittext下(不是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" android:focusableInTouchMode="true"android:fitsSystemWindows="true"或者在與edittext布局分開的地方添加這個textview,將framelayout作為root,

但問題有點復雜。

另一個重要的事情是, edittext 下面的這個文本應該

  • 當文本在另一行上時保持在鍵盤上方

  • 當 edittext 向上滾動時消失(因為它是 edittext 的一部分)。

您也可以使用此設計存檔您的設計

在 XML 中使用TextInputLayout instated of 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

快樂學習...

我遇到了同樣的問題。 我已經在 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)

我希望這對你有幫助!

請將此app:counterEnabled="true " app:counterMaxLength="250"到您的TextInputLayout

<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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM