我只是希望通過文本的值更改按鈕的背景顏色。例如,當我將文本更改爲:Android如何通過文本更改按鈕顏色
button.setText("YES");
我想按鈕的背景顏色設置爲綠色。 當我將文本更改爲:
button.setText("NO");
我想按鈕的背景顏色設置爲紅色。
當我改變它在Java代碼:
boolean textValueYES = true;
button.setBackgroundColor(textValueYES ? Color.GREEN : Color.RED);
按鈕失去其drawable.xml設置。 有沒有辦法將這種檢查添加到可繪製的XML? 或者通過文本值設置背景顏色而不丟失可繪製設置?
要設置按鈕背景繪製使用'button.setBackgroundResource(textValueYES? R.drawable.greendrawable:R.drawable.reddrawable);' – AKSiddique
創建帶有紅色背景色和綠色背景色的可繪製文件,如:red_drawable.xml,green_drawable.xml,並使用button.setBackgroundResource(R.drawable.red_drawable); –