android edittext 输入字数限制 超过最大字数toast
16lz
2021-01-23
1.不实时显示已经写入字体
etEvaluateContent.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().trim().length() > maxLength) { etEvaluateContent.setText(s.toString().substring(0, maxLength)); etEvaluateContent.setSelection(maxLength); ToastUtil.showToastWithImg("您最多能输入200个字", R.mipmap.toast_error); } } @Override public void afterTextChanged(Editable s) { } });
2.实时显示已经写入字体个数
etSuggestContent.addTextChangedListener(new TextWatcher() { CharSequence input; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { input = s; } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { tvMaxCount.setText(String.format("%d/200", input.length())); if (input.length() > 199) { ToastUtil.showToastWithImg("您最多能输入200个字", R.mipmap.toast_error); } } @Override public void afterTextChanged(Editable s) { } });
注意:以上两种类型方案均需要在xml文件中设置最大字数属性(maxLength)。
"@+id/et_suggest_content" style="@style/text_normal_light" android:layout_width="0dp" android:layout_height="141dp" android:layout_marginTop="@dimen/dp_10" android:background="@color/white" android:gravity="top" android:hint="您可以在这里给我们建议帮助我们做的更好" android:maxLength="200" android:paddingBottom="@dimen/dp_27" android:paddingLeft="@dimen/app_margin" android:paddingRight="@dimen/app_margin" android:paddingTop="@dimen/dp_10" android:textColorHint="@color/hint" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/>
更多相关文章
- Android TextView字体设置
- Android实时采集视频,并在PC上显示出采集到的视频的程序
- CSS字体随键盘弹出而改变
- Android Studio 实时显示布局文件Preview窗口
- textview设置字体以及引入新字体
- 使用ThinDownloadManager下载apk以及notification实时展示进度和
- Android 录音获取麦克风实时音量
- Android - Android Studio修改字体(font)大小(size)
- android studio 中文乱码,字体优化解决之道