TextInputLayout setCounterEnables不起作用 - Android

时间:2017-07-08 19:07:26

标签: android android-textinputlayout

我有一个带有<?xml version="1.0" encoding="utf-8"?> <ScrollView 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:background="@color/colorBackground" android:paddingLeft="@dimen/create_step_padding" android:paddingRight="@dimen/create_step_padding" android:paddingTop="@dimen/create_step_padding"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Shipper Text Area--> <android.support.design.widget.TextInputLayout android:id="@+id/shipper_layout" android:layout_width="0dp" android:layout_height="110dp" android:background="@drawable/text_area_background" android:paddingEnd="@dimen/text_input_layout_side_padding" android:paddingStart="@dimen/text_input_layout_side_padding" android:paddingTop="@dimen/text_input_layout_top_padding" app:counterEnabled="true" app:hintTextAppearance="@style/HintTextStyle" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <android.support.design.widget.TextInputEditText android:id="@+id/shipper_field" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:gravity="top|start" android:hint="@string/shipper_field" android:inputType="textMultiLine" android:lines="5" android:overScrollMode="always" android:scrollbarStyle="insideInset" android:scrollbars="vertical" /> </android.support.design.widget.TextInputLayout> <!-- Consignee Text Area--> <android.support.design.widget.TextInputLayout android:id="@+id/consignee_layout" android:layout_width="0dp" android:layout_height="110dp" android:layout_marginTop="16dp" android:background="@drawable/text_area_background" android:paddingLeft="@dimen/text_input_layout_side_padding" android:paddingTop="@dimen/text_input_layout_top_padding" app:counterEnabled="true" app:hintTextAppearance="@style/HintTextStyle" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/shipper_layout"> <android.support.design.widget.TextInputEditText android:id="@+id/consignee_field" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:gravity="top|start" android:hint="@string/consignee_field" android:inputType="textMultiLine" android:lines="5" android:overScrollMode="always" android:scrollbarStyle="insideInset" android:scrollbars="vertical" /> </android.support.design.widget.TextInputLayout> <!-- Airport of Departure --> <!-- Airport of Destination --> <com.silverfix.dgdeditor.utils.views.AirportAutoCompleteEditText android:id="@+id/aodep_field" android:layout_width="217dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintBottom_toBottomOf="@+id/textView11" app:layout_constraintHorizontal_bias="0.512" app:layout_constraintLeft_toRightOf="@+id/textView11" app:layout_constraintRight_toRightOf="parent" /> <com.silverfix.dgdeditor.utils.views.AirportAutoCompleteEditText android:id="@+id/aodes_field" android:layout_width="217dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintBottom_toBottomOf="@+id/textView12" app:layout_constraintLeft_toLeftOf="@+id/aodep_field" /> <TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="32dp" android:text="@string/aodep_field" android:textAppearance="@style/DefaultTextStyle" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/consignee_layout" /> <TextView android:id="@+id/textView12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="@string/aodes_field" android:textAppearance="@style/DefaultTextStyle" app:layout_constraintLeft_toLeftOf="@+id/textView11" app:layout_constraintTop_toBottomOf="@+id/textView11" /> </android.support.constraint.ConstraintLayout> </ScrollView> 个元素的布局,我想启用字符计数器。问题是它不起作用,我在运行应用程序时无法看到计数器。

这是我的布局:

app:counterEnabled

如您所见,我在TextInputLayout中启用了shipperTextArea = (TextInputLayout) rootView.findViewById(R.id.shipper_layout); consigneeTextArea = (TextInputLayout) rootView.findViewById(R.id.consignee_layout); shipperTextArea.setCounterMaxLength(60); consigneeTextArea.setCounterMaxLength(60); 。 然后我尝试在代码中设置max chars。

以下是代码:

Map

1 个答案:

答案 0 :(得分:0)

只需在xml文件中设置计数器,添加:

<android.support.design.widget.TextInputLayout
    app:counterMaxLength="60"
    app:counterEnabled="true"

我认为不需要在java类中设置。