Button根據(jù)EditText輸入狀態(tài)改變背景顏色

需求

Button隨EditText輸入狀態(tài)改變顏色

有3個不同顏色狀態(tài),

  • EditText未輸入時,Button處于不可點擊狀態(tài)
  • EditText輸入時,Button處于高亮狀態(tài)
  • EditText輸入且用戶按下按鈕,Button --> Pressed狀態(tài)

效果如下:


演示圖片

EditText在沒有輸入時,Button不可點擊,為灰色狀態(tài)
EditText輸入后,Button可點擊,且背景變?yōu)樗{(lán)色
EditText輸入后,點擊Button時,Button背景色變?yōu)榧t色

解決思路

EditText的輸入通過添加addTextChangedListener來監(jiān)聽
Button的點擊顏色變化使用selector來控制

遇到的問題

在根據(jù)以上的實現(xiàn)思路實現(xiàn)時,遇到了一些問題

問題一:在Selector中使用android:color屬性報錯
button_selector.xml代碼:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_red_light" android:state_pressed="true"/>
    <item android:color="@android:color/darker_gray"/>
</selector>

應(yīng)用崩潰的錯誤日志:

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: <item> tag requires a 'drawable' attribute or child tag defining a drawable

日志提示在item子節(jié)點中必須要求有drawable屬性,根據(jù)錯誤信息將所有color屬性替換成了drawable,修改后的button_selector.xml如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/holo_red_light" android:state_pressed="true"/>
    <item android:drawable="@android:color/darker_gray"/>
</selector>

問題二:selector沒有作用,Button按下時顏色并沒有改變
給Button的background屬性設(shè)置了button_selector
然后在EditText. addTextChangedListener中的onTextChanged方法中檢測EditText的輸入狀態(tài)

        editText.addTextChangedListener(new 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) {
                //EditText輸入狀態(tài)改變,Button背景顏色也改變
                if ("".equals(editText.getText().toString().trim())) {
                    button.setBackgroundColor(Color.GRAY);
                    button.setEnabled(false);
                } else {
                    button.setBackgroundColor(ContextCompat.getColor(context, R.color.color_blue));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

在EditText中輸入字符后,Button背景色變?yōu)樗{(lán)色,但是pressed時卻沒有變成紅色,背景還是藍(lán)色,發(fā)現(xiàn)是button.setBackgroundColor(ContextCompat.getColor(context, R.color.color_blue));把Button的背景色給寫死了,所以Button的顏色沒辦法改變

解決方案

整理了下問題,最后想到了一個解決方案,在布局文件中,把Button的background的屬性由selector設(shè)置為不可點擊顏色灰色android:background="@android:color/darker_gray",然后在onTextChanged()中,當(dāng)EditText輸入時,設(shè)置Button的background為selector,而不是寫死顏色,這樣就可以解決在EditText輸入時,點擊Button背景顏色卻無法變化的問題!

        editText.addTextChangedListener(new 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) {
                //EditText輸入狀態(tài)改變,Button背景顏色也改變
                if ("".equals(editText.getText().toString().trim())) {
                    button.setBackgroundColor(Color.GRAY);
                    button.setEnabled(false);
                } else {
                    //設(shè)置selector來控制Button背景顏色
                    button.setBackground(ContextCompat.getDrawable(context,
                            R.drawable.button_input_selector));
                    button.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

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

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

  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,358評論 0 17
  • 你是否曾抱怨過產(chǎn)品經(jīng)理,為什么一個app里面按鈕正常/按下狀態(tài)顏色不統(tǒng)一起來?你是否曾埋怨過UI,為什么不同地方輸...
    m_博客閱讀 711評論 1 9
  • 引言 EditTex是Android中比較常用的一個控件,可以說它是用戶和Android應(yīng)用進(jìn)行數(shù)據(jù)傳遞的通道.通...
    OzanShareing閱讀 6,625評論 5 33
  • 讀書的第一層境界是你看得懂別人的道理,第二層境界是自己能照著講出來,第三層境界是自己能有意識的去運用,第四層境界是...
    藝hongu閱讀 287評論 0 0
  • 千萬不要低效的努力! 2017-03-14 17:30 請點擊此處輸入圖片描述 她是我初中同學(xué),人長得漂亮,性格也...
    跟娟姐學(xué)蛻變閱讀 595評論 0 1

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