android - 在输入过程中显示字符数

标签 android xml android-layout android-relativelayout

我有一个用于描述的 EditText。

在它下面,我有一个 TextView 显示在 EditText 中输入的字符数。

例子:

enter image description here

用户在输入的时候应该可以看到活字符数,但是此时字符计数器被键盘隐藏了:

enter image description here

我该怎么做才能纠正它?

这是我的 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/

相关文章:

android - 在 Android 中单击按钮后使布局出现动画

Android 通知未按预期工作

android - “App_Name” 由于错误 (500) 无法下载?

android - ConstraintLayout 中的 selectableItemBackgroundBorderless 问题

android - 带分页的 TableLayout

c# - 使用XPath读取xml

xml - 使用 XSLT 复制 XML 中的所有节点,支持特殊情况

html - 如何向 Blogger 博客上的页面添加单独的唯一正文类

android - 在 onCreateView 中向 listFragment 添加静态 header

android - 如何在 RelativeLayout 中将一个 View 放置在另一个 View 之上?