Android---监听EditText变化

可以使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,重写三个方法:
这里写图片描述

1.MainActivity.java

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

public class MainActivity extends Activity implements OnClickListener{

    private TextView tv_max;
    private EditText et_tel;
    private Button btn_clear;
    private static final int MAX = 11;
    private int left = MAX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_max = (TextView) findViewById(R.id.tv_max);
        et_tel = (EditText) findViewById(R.id.et_tel);

        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(this);

        et_tel.addTextChangedListener(watcher);
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("TAG", "onTextChanged()");
            if(left > 0){
                left = MAX - et_tel.getText().length();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.d("TAG", "beforeTextChanged()");
            tv_max.setText("还能输入" + left + "个数字");
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("TAG", "afterTextChanged()");
            tv_max.setText("还能输入" + left + "个数字");
        }
    };

    @Override
    public void onClick(View v) {
        et_tel.setText("");
        left = MAX;
        tv_max.setText("还能输入" + left + "个数字");
    }

}

2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.textwatcher.MainActivity" >

    <TextView 
        android:id="@+id/tv_max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="还能输入11个数字"
        />

    <EditText
        android:id="@+id/et_tel"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:padding="10dp"
        android:textSize="19sp"
        android:layout_below="@id/tv_max"
        android:hint="请输入手机号"
        android:background="@null"
        android:inputType="phone"/>

    <View 
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E0E0E0"
        android:layout_below="@id/et_tel"
        />

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view"
        android:text="清空"
        />

</RelativeLayout>

效果预览:
这里写图片描述

转载自:http://liangruijun.blog.51cto.com/3061169/729505

源码下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值