對一個TextView的文本按鈕動態的設置字體的顏色和背景的顏色

定義好你的背景資源文件,當這個CheckBox組件被勾選時,設置TextView按鈕的字體顏色和TextView組價的背景:

        //這是一個TextView的按鈕
        TextView confirmBtn = findViewById(R.id.confirm);
        //這是一個CheckBox組件,當這個組件被勾選時,設置上面TextView按鈕的字體顏色和背景
        CheckBox agreeCheckBox = findViewById(R.id.checkbox_agree);

        agreeCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //設置TextView按鈕的背景顏色
                confirmBtn.setBackgroundResource(isChecked ? R.drawable.backgroudbluebtn : R.drawable.backgroudblue_unclick);
                 //設置TextView按鈕的字體顏色
                confirmBtn.setTextColor(isChecked? Color.parseColor("#FF4489F7"):Color.parseColor("#71BAF1"));
            }
        });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章