Android android.text.TextWatcher详解

When an object of a type is attached to an Editable, its methods will be called when the text is changed.

当一个可编辑的对象的文本改变的时候,它的方法就会被调用。

此接口有三个方法

afterTextChanged(Editable s)

This method is called to notify you that, somewhere within s, the text has been changed.

当s的某个位置的文本已经改变,这个方法会被调用

beforeTextChanged(CharSequence s, int start, int count, int after)

This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after.

在s中,当从start开始的count个字符将被长度为after的新文本替换时,这个方法会被调用

onTextChanged(CharSequence s, int start, int before, int count)

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.

在s中,当从start开始有count个字符替换长度为before的旧文本时,这个方法会被调用


下面是一个限制输入"."个数的例子

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    EditText et_content;
    TextView tv_numOfChar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_content = (EditText)findViewById(R.id.et_content);
        tv_numOfChar = (TextView)findViewById(R.id.tv_numOfChar);
        et_content.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("log","beforeTextChanged:" + s + "------" + start + "------" + after + "------"+ count);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("log","onTextChanged:" + s + "------" + start + "------"+ before+ "------" + count);
                if (et_content.getText().toString().indexOf(".") >= 0) {
                    tv_numOfChar.setText("已经输入\".\"不能重复输入");
                        if (et_content.getText().toString().indexOf(".", et_content.getText().toString().indexOf(".") + 1) > 0) {
                            et_content.setText(et_content.getText().toString().substring(0, et_content.getText().toString().length() - 1));
                            et_content.setSelection(et_content.getText().toString().length());
                        }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("log","afterTextChanged:" + s);
            }
        });
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值