如何解决如何在editText框的右下角显示editText字符限制??下面的例子
上图显示了一个editext,它的文本限制显示在右下角,250 个字符。随着用户在 editText 中键入字符,“250”会减少。
我一直在尝试将其实现到我的编辑文本中,但这有点具有挑战性。我尝试了各种东西。 只是为了让您知道我正在使用一个 alertDialog,其中包含 editText。然后 alertDialog 显示一个 xml,其中包含 editText 属性。
感谢您的帮助!!!!
解决方法
使用 android 材料这很容易。我希望它有所帮助:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true" --this makes counter enable
app:counterMaxLength="20" -- this is the limittion
android:hint="@string/label">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
或者如果你只想要一个 EditeText,你可以使用 TextWatcher:
private TextView mTextView;
private EditText mEditText;
mTextView.setText("250");
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s,int start,int count,int after) {
}
public void onTextChanged(CharSequence s,int before,int count) {
//This sets a textview to the current length
mTextView.setText(String.valueOf(250-s.length()));
}
public void afterTextChanged(Editable s) {
}
};
mEditText.addTextChangedListener(mTextEditorWatcher);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。