In development, we often encounter this situation

What we usually want is the following effect

However, if the implementation of the code to write in the Activity will be more troublesome, affecting the code aesthetics

This leads to the following helper class, which disables button click events and button gradients and adds one or more EditTexts simultaneously

/** * Text input helper class, Enable/disable button click events */ Public Final Class TextInputHelper implements TextWatcher {private View  mMainView; // View private List<TextView> mViewSet; Private Boolean isAlpha; // Select EditText, TextView, Button. Public TextInputHelper(View View) {this(View,true); } /** * the @param view constructor ** follows the EditText or TextView input as empty to determine whether to enable or disable the view * @param alpha requires transparency */ public TextInputHelper(View view, boolean alpha) {if (view == null) throw new IllegalArgumentException("The view is empty"); mMainView = view; isAlpha = alpha; } /** * add EditText or TextView listener ** @param Views passes one or more EditText or TextView objects */ public void addViews(TextView... views) {if (views == null) return;

        if (mViewSet == null) {
            mViewSet = new ArrayList<>(views.length - 1);
        }

        for(TextView view : views) { view.addTextChangedListener(this); mViewSet.add(view); } afterTextChanged(null); } /** * remove EditText listener to avoid memory leak */ public voidremoveViews() {
        if (mViewSet == null) return;

        for (TextView view : mViewSet) {
            view.removeTextChangedListener(this);
        }
        mViewSet.clear();
        mViewSet = null;
    }

    // TextWatcher

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public synchronized void afterTextChanged(Editable s) {
        if (mViewSet == null) return;

        for (TextView view : mViewSet) {
            if ("".equals(view.getText().toString())) {
                setEnabled(false);
                return; }}setEnabled(true); } /** * @param Enabled Enables or disables View events */ public voidsetEnabled(boolean enabled) {
        if (enabled == mMainView.isEnabled()) return;

        if(enabled) {// Enable the View event mmainView.setenabled (true);
            if(isAlpha) {// Set opacity to mmainView.setalpha (1f); }}else{// Disable the View event mmainView.setenabled (false);
            if(isAlpha) {// Set translucence to mmainView.setalpha (0.5f); }}}}Copy the code

Add a listener at Activity creation time

private TextInputHelper mInputHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View mInputHelper = new TextInputHelper(mButton); MInputHelper. AddViews (mEditText1, mEditText2, mEditText3); }Copy the code

Remove listeners when the Activity is destroyed (to avoid memory leaks)

@Override
protected void onDestroy() { super.onDestroy(); // Remove references to avoid memory leaks minputhelper.removeviews (); }Copy the code

Notice that not only can you add EditText, you can also add TextView, because EditText is a subclass of TextView, and at the end of the day, if you think it’s going to work, give it a thumbs up.

Creation is not easy to like, remember to click + attention oh