我有一个用于描述的 EditText。
在它下面,我有一个 TextView 显示在 EditText 中输入的字符数。
例子:
用户在输入的时候应该可以看到活字符数,但是此时字符计数器被键盘隐藏了:
我该怎么做才能纠正它?
这是我的 xml 代码:
<EditText
android:id="@+id/MyDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:maxLength="500"
android:hint="Description" />
<TextView
android:id="@+id/MyDescriptionCharCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/500"
android:layout_below="@id/MyDescription"
android:layout_marginTop="5dp"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
父级是一个 RelativeLayout。
最佳答案
您必须使用 TextWatcher 扩展类并覆盖 afterTextChanged()、beforeTextChanged()、onTextChanged()。
EditText MyDescription = (EditText)findViewById(R.id. MyDescription);
MyDescription.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s.toString().trim().length();
}
});
你可以试试下面的布局。
<RelativeLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:background="@android:color/transparent"
android:inputType="textNoSuggestions|textMultiLine"
android:maxLength="200"
android:paddingBottom="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:textColor="#202020"
android:textColorHint="#979797"
android:textSize="14sp"/>
<TextView
android:id="@+id/charecter_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/add_comment_edit_text"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:gravity="bottom"
android:text="0/200"
android:textColor="#979797"
android:textSize="14sp"/>
</RelativeLayout>
关于android - 在输入过程中显示字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286947/