备份Edittext编辑框字数限制ui以及逻辑
Posted 六道对穿肠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了备份Edittext编辑框字数限制ui以及逻辑相关的知识,希望对你有一定的参考价值。
ui布局
etContent.addTextChangedListener(new TextWatcher()
//记录输入的字数
private CharSequence enterWords;
private int selectionStart;
private int selectionEnd;
private int enteredWords;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
//实时记录输入的字数
enterWords = s;
@Override
public void afterTextChanged(Editable s)
//已输入字数
enteredWords = wordLimitNum - s.length();
//TextView显示剩余字数
tvEndNumber.setText(wordLimitNum - enteredWords + "/" + wordLimitNum);
);
edittext_background_number.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="6dp" />
<solid android:color="@color/color_F5F5F5"/>
<stroke
android:width="1dp"
android:color="#fff2f3f7" />
</shape>
监听字数
//记录字数上限
int wordLimitNum = 50;
etContent.addTextChangedListener(new TextWatcher()
//记录输入的字数
private CharSequence enterWords;
private int selectionStart;
private int selectionEnd;
private int enteredWords;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
//实时记录输入的字数
enterWords = s;
@Override
public void afterTextChanged(Editable s)
//已输入字数
enteredWords = wordLimitNum - s.length();
//TextView显示剩余字数
tvEndNumber.setText(50 - enteredWords + "/50");
selectionStart = etContent.getSelectionStart();
selectionEnd = etContent.getSelectionEnd();
if (enterWords.length() > wordLimitNum)
//删除多余输入的字(不会显示出来)
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
etContent.setText(s);
//设置光标在最后
etContent.setSelection(tempSelection);
);
键盘遮挡问题:
android:windowSoftInputMode="adjustPan"
以上是关于备份Edittext编辑框字数限制ui以及逻辑的主要内容,如果未能解决你的问题,请参考以下文章