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

源碼下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章