preface

An article without a foreword always feels like something is missing. This article is purely for backup.

Introduction to the

Multiple input boxes control the state of a button control at the same time, and to improve the quality and cohesion of code, auxiliary tools were created. The implementation of this tool refers to a blogger’s article, the link to the article can not be found, that is a blogger. No amount of introduction is worth a rendering:

rendering

If only one is entered, press Enable = false

If only the input field has content, the enable = true button is used

Method of use

In order to avoid memory leaks caused by EditText calling addTextChangedListener() without unlogging the listener, the assistive tool has added a destruction method to remove the listener by calling removeViews() in onDestroy.

1. First declare the object

/** ** Button style control tool */
TextInputUtils nextHelper;
Copy the code

2. Instantiate the object

BtnNext is the controlled control

nextHelper = new TextInputUtils(btnNext);
Copy the code

3. Add control controls

addViews(TextView… View) with a variable length argument list. Enable = true for the controlled control when all controls in the parameter meet the condition.

nextHelper.addViews(etNumber, etCode);
Copy the code

4. Release the control

nextHelper.removeViews();
Copy the code

The source code in this

package com.momin.baselib.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/** * <p>Title: EditTextInputHelper</p> * <p>Description: Edit box </p> * <p>Copyright: </p> * <p>Company: </p> **@author Momin
 * @version 1.0
 * @date2020/9/25 16:03 * /

public class TextInputUtils implements TextWatcher {

    /** * The operated object */
    private View targetView;
    /** * listen to set */
    private List<TextView> viewSet;

    public TextInputUtils(View view) {
        if (view == null) {
            throw new IllegalArgumentException("The view is empty");
        }

        this.targetView = view;
    }

    /** * add EditText listener *@paramViews passes in one or more TextViews/EditText objects */
    public void addViews(TextView... views) {
        if (views == null) {
            return;
        }
        if (viewSet == null) {
            viewSet = new ArrayList<>(views.length - 1);
        }
        for (TextView view : views) {
            view.addTextChangedListener(this);
            viewSet.add(view);
        }
        afterTextChanged(null);
    }


    /** * Remove the TextWatcher listener to avoid memory leaks */
    public void removeViews(a) {
        if (viewSet == null) {
            return;
        }
        for (TextView view : viewSet) {
            view.removeTextChangedListener(this);
        }
        viewSet.clear();
        viewSet = null;
    }

    @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) {
        if (viewSet == null) {
            return;
        }

        for (TextView view : viewSet) {
            if ("".equals(view.getText().toString())) {
                setEnabled(false);
                return;
            }
        }

        setEnabled(true);
    }

    /** * Set the View event *@paramEnabled Enables or disables the View event */
    public void setEnabled(boolean enabled) {
        if (enabled == targetView.isEnabled()) {
            return;
        }
        if (enabled) {
            // Enable the View event
            targetView.setEnabled(true);
        }else {
            // Disable View events
            targetView.setEnabled(false); }}}Copy the code