Android 监听EditText 变化

08 112016

1、实现效果:当注册登陆时文本框内容长度大于1时出现 一个全部清空按钮。如图:


blob.png

2、实现思路:实时监听EditText内容变化,当长度大于1,设置ImageView显示,当长度为0,ImageView隐藏


3、具体代码:


//Acitivty代码

public class LoginActivity extends AppCompatActivity implements TextWatcher{

    private EditText _login_phone;
    private EditText _login_pwd;
    private ImageView _login_iv_1;
    private ImageView _login_iv_2;

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

        _login_phone = (EditText)findViewById(R.id.login_phone);
        _login_pwd   = (EditText)findViewById(R.id.login_pwd);
        _login_iv_1  = (ImageView)findViewById(R.id.login_iv_1);
        _login_iv_2  = (ImageView)findViewById(R.id.login_iv_2);

        _login_phone.addTextChangedListener(this);
        _login_pwd.addTextChangedListener(this);


    }
    
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    @Override
    public void afterTextChanged(Editable editable) {
        
        //login_iv_1
        if(_login_phone.getText().length() >= 1){    //获取内容长度
            _login_iv_1.setVisibility(View.VISIBLE);  //显示
        }else{
            _login_iv_1.setVisibility(View.INVISIBLE); //隐藏
        }
        
        //login_iv_2
        if(_login_pwd.getText().length() >= 1){
            _login_iv_2.setVisibility(View.VISIBLE);
        }else{
            _login_iv_2.setVisibility(View.INVISIBLE);
        }    
        
    }

    @Override
    public void onClick(View view) {
            
    }

}


//Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:background="@color/colorBg"
    tools:context=".RegisterActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/login_phone"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:paddingLeft="10dp"
            android:background="@null"
            android:singleLine="true"
            android:inputType="phone"
            android:hint="请输入手机号"/>

        <ImageView
            android:id="@+id/login_iv_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_pwd_delete"
            android:layout_gravity="center"
            android:visibility="invisible"
            android:layout_marginRight="8dp"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/login_pwd"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="10dp"
            android:layout_weight="3"
            android:singleLine="true"
            android:background="@null"
            android:inputType="textPassword"
            android:hint="请输入密码"/>

        <ImageView
            android:id="@+id/login_iv_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_pwd_delete"
            android:layout_gravity="center"
            android:visibility="invisible"
            android:layout_marginRight="8dp"/>

    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="50dp"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center|center_horizontal|center_vertical"
        >

        <Button
            android:id="@+id/button_login"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:textColor="@color/colorWhite"
            android:textStyle="normal"
            android:layout_gravity="center"
            android:background="@drawable/register_button_selector"
            android:text="登录"/>

    </LinearLayout>


</LinearLayout>



版权信息

原文作者: 石头记

原文链接: http://1m85.com/12.html

版权所有: 本文为博主原创文章,转载请标明出处。