全屏 TextInputEditText 不显示计数器

3

我正在尝试将TextInputEditText适应整个屏幕,但是这并不是没有问题:

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

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxLength="1000"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

输入类型:

TextInputEditText tv = findViewById( R.id.edit_text );
tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );

结果是计数器消失了,提示出现在中间。如果我改为自动换行,它就恢复正常行为。示例:

enter image description here

所以基本上只要显示计数器,将提示对齐到顶部是次要的。

计数器也在EditText中吗? - Umair
@Umair 试过了,还是一样。 - RonTLV
我只是问了一个问题,并没有让你做任何事情 ;) - Umair
“the counter”是什么意思?你能分享一下你认为的“正常行为”的截图吗? - Stachu
@Stachu 示例计数器 - RonTLV
使用 EditText 与使用 TextInputEditText 相比,前者能够按预期工作 - 使用后者的特别原因是什么? - Stachu
5个回答

7

尝试使用权重属性而不是match_parent属性。android:layout_weight="1" 计数器将可见。

当我们点击EditText或EditText处于焦点时,提示文本对齐方式是正确的。由于EditText处于全屏状态,请求焦点可以解决提示文本对齐问题。

 TextInputEditText tv = findViewById( R.id.edit_text );
 tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );
 tv.requestFocus();

你的布局将如下所示:

<LinearLayout
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:counterTextColor="@color/black"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLength="1000"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>`

android:layout_weight="1" 可以使用,但是您需要将 TextInputEditTextandroid:layout_height="wrap_content" 设置为 match_parent 以适应整个屏幕。 - ljk

1

Screenshot

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:passwordToggleEnabled="false"
        app:endIconMode="none">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxLength="1000"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>
        <TextView
            android:id="@+id/txtcount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:textColor="@color/colorAccent"
            android:textSize="10sp"
            android:visibility="gone"
            android:text="24"/>
    </RelativeLayout>
</LinearLayout>

活动

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    if(StringUtils.isNotEmpty(editText.getText().toString().trim())) {
                        txtcount.setVisibility(View.VISIBLE);
                        txtcount.setText(String.valueOf(editText.getText().length()));
                    }else {
                        txtcount.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    txtcount.setVisibility(View.GONE);
                    e.printStackTrace();
                }

            }
        });

0
只需将 android:layout_weight="1" 添加到你的 TextInputEditText 中即可。这个技巧对我有效。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:counterOverflowTextAppearance="@dimen/_20sdp"
       >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:maxLength="1000"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

希望这对你有所帮助!!

0

我已经测试过,在我的设备上运行良好:

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


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:passwordToggleEnabled="false"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/edit_text"
            android:hint="Type here..."
            android:maxLength="1000"
            android:gravity="top|left"/>

    </com.google.android.material.textfield.TextInputLayout>


</LinearLayout>

我的设备(OnePlus 6 Android 10)和模拟器(Android 7.1)上都没有显示计数器。 - RonTLV

0

我会改用EditText而不是TextInputEditText

<?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="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:counterEnabled="true"
        app:counterMaxLength="1000"
        app:endIconMode="none"
        app:passwordToggleEnabled="false">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Type here..."
            android:inputType="textPersonName"
            android:maxLength="1000" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

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