Android多個(gè)EditText輸入效果解決方案

在開發(fā)中,我們常常遇到這種情況

image.png

我們往往需要的是下面這種效果

image.png

但是如果把這些實(shí)現(xiàn)的代碼寫在Activity中會(huì)比較麻煩,影響代碼美觀

于是就有了下面這個(gè)輔助類,禁用了按鈕的點(diǎn)擊事件和按鈕的漸變色,可同時(shí)添加一個(gè)或者多個(gè)EditText

/**
 * 文本輸入輔助類,通過管理多個(gè)TextView或者EditText輸入是否為空來啟用或者禁用按鈕的點(diǎn)擊事件
 */
public final class TextInputHelper implements TextWatcher {

    private View mMainView;//操作按鈕的View
    private List<TextView> mViewSet;//TextView集合,子類也可以(EditText、TextView、Button)
    private boolean isAlpha;//是否設(shè)置透明度

    public TextInputHelper(View view) {
        this(view, true);
    }

    /**
     * 構(gòu)造函數(shù)
     *
     * @param view              跟隨EditText或者TextView輸入為空來判斷啟動(dòng)或者禁用這個(gè)View
     * @param alpha             是否需要設(shè)置透明度
     */
    public TextInputHelper(View view, boolean alpha) {
        if (view == null) throw new IllegalArgumentException("The view is empty");
        mMainView = view;
        isAlpha = alpha;
    }

    /**
     * 添加EditText或者TextView監(jiān)聽
     *
     * @param views     傳入單個(gè)或者多個(gè)EditText或者TextView對象
     */
    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);
    }

    /**
     * 移除EditText監(jiān)聽,避免內(nèi)存泄露
     */
    public void removeViews() {
        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);
    }

    /**
     * 設(shè)置View的事件
     *
     * @param enabled               啟用或者禁用View的事件
     */
    public void setEnabled(boolean enabled) {
        if (enabled == mMainView.isEnabled()) return;

        if (enabled) {
            //啟用View的事件
            mMainView.setEnabled(true);
            if (isAlpha) {
                //設(shè)置不透明
                mMainView.setAlpha(1f);
            }
        }else {
            //禁用View的事件
            mMainView.setEnabled(false);
            if (isAlpha) {
                //設(shè)置半透明
                mMainView.setAlpha(0.5f);
            }
        }
    }
}

在Activity創(chuàng)建時(shí)添加監(jiān)聽

private TextInputHelper mInputHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //創(chuàng)建一個(gè)輔助類,傳入按鈕操作View
    mInputHelper = new TextInputHelper(mButton);
    //可添加一個(gè)或者多個(gè)EditText,當(dāng)然也可以添加TextView
    mInputHelper.addViews(mEditText1, mEditText2, mEditText3);
}

在Activity銷毀時(shí)移除監(jiān)聽(避免內(nèi)存泄露)

@Override
protected void onDestroy() {
    super.onDestroy();

    //移除引用,避免內(nèi)存泄露
    mInputHelper.removeViews();
}

需要注意的是這里不單單只是可以添加EditText,還可以添加TextView,因?yàn)镋ditText是TextView的子類,最后要是覺得好用記得點(diǎn)個(gè)贊。

創(chuàng)作不易喜歡的話記得點(diǎn)擊+關(guān)注哦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容