【问题标题】:Make character count visible during input在输入期间使字符数可见
【发布时间】:2017-07-06 07:57:18
【问题描述】:

我有一个用于描述的 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。

【问题讨论】:

标签: android xml android-layout android-relativelayout


【解决方案1】:

您必须使用 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>

【讨论】:

  • 程序如何知道我要显示字符计数器?我的字符计数器下方还有许多其他布局
  • 改写你的评论。
  • 对不起,我不明白之后键盘在我的字符计数器下会怎样,你能解释一下吗?
  • 我的并且是关于如何跟踪编辑文本中输入字符的计数。要在键盘上方显示计数器,您必须相应地调整布局。
【解决方案2】:

要查看字符计数器的文本视图,请在清单文件的activity 标记中添加以下行。

android:windowSoftInputMode="adjustResize|stateAlwaysHidden"

这将在添加软输入键盘后自动将您的活动布局向上移动以固定可用高度。有关此检查的更多详细信息here.

【讨论】:

  • 程序如何知道我要显示字符计数器?我的字符计数器下方还有许多其他布局
猜你喜欢
  • 2020-02-15
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 2012-10-28
  • 2014-01-12
  • 2013-07-12
相关资源
最近更新 更多