android editText输入文本框限制输入字数和字数提示
2013-09-17 10:55
537 查看
EditText content;//定义一个文本输入框
TextView hasnum;// 用来显示剩余字数
int num
= 140;//限制的最大字数
content =
(EditText) findViewById(R.id.et_content);
hasnumTV =
(TextView) findViewById(R.id.tv_num);
hasnumTV.setText(num+"");
//下面为EditText文本框添加监听
content.addTextChangedListener(new
TextWatcher()
{
private
CharSequence temp;
private
int selectionStart;
private
int selectionEnd;
public
void beforeTextChanged(CharSequence s,
int start,
int count,
int after)
{
}
public
void onTextChanged(CharSequence s,
int start,
int before,
int count)
{
temp = s;
}
public
void afterTextChanged(Editable s)
{
int number
= num - s.length();
hasnumTV.setText(""
+ number);
selectionStart = content.getSelectionStart();
selectionEnd = content.getSelectionEnd();
if (temp.length()
> num)
{
s.delete(selectionStart
- 1, selectionEnd);
int tempSelection
= selectionEnd;
content.setText(s);
content.setSelection(tempSelection);//设置光标在最后
}
}
});
TextView hasnum;// 用来显示剩余字数
int num
= 140;//限制的最大字数
content =
(EditText) findViewById(R.id.et_content);
hasnumTV =
(TextView) findViewById(R.id.tv_num);
hasnumTV.setText(num+"");
//下面为EditText文本框添加监听
content.addTextChangedListener(new
TextWatcher()
{
private
CharSequence temp;
private
int selectionStart;
private
int selectionEnd;
public
void beforeTextChanged(CharSequence s,
int start,
int count,
int after)
{
}
public
void onTextChanged(CharSequence s,
int start,
int before,
int count)
{
temp = s;
}
public
void afterTextChanged(Editable s)
{
int number
= num - s.length();
hasnumTV.setText(""
+ number);
selectionStart = content.getSelectionStart();
selectionEnd = content.getSelectionEnd();
if (temp.length()
> num)
{
s.delete(selectionStart
- 1, selectionEnd);
int tempSelection
= selectionEnd;
content.setText(s);
content.setSelection(tempSelection);//设置光标在最后
}
}
});
相关文章推荐
- android EditText 对输入字数和内容范围进行限制
- Android edittext 显示字数限制和输入类型
- EditText 设置字数限制,超过字数限制时 toast 提示并且输入无效
- Android EditText多行文本输入 字数统计 限制数量
- android edittext 输入字数限制 超过最大字数toast
- Android EditText限制输入字数
- android editText 输入字数限制
- 对EditText输入字数限制并提示
- editText提示输入文字字数,同时限制字数和清空输入内容
- Android EditText限制输入字数的方法
- Android EditText限制文本输入的长度并给予提示
- Android editText 输入字数限制
- android editText 输入字数限制
- Android EditText输入字数限制总结(包括中文输入内存溢出的解决方法)
- android edittext 输入长度限制并提示
- Android editText 输入字数限制
- android EditText限制输入字数的方法
- Android开发中给EditText控件添加TextWatcher监听实现对输入字数的限制(推荐)
- android edittext 输入长度限制并提示
- android EditText限制输入的字数