Change clickable TextView's color on focus and click?

后端 未结 8 1489
灰色年华
灰色年华 2020-11-28 05:26

I have a clickable TextView that I want to give some colors to. But I don\'t know how. Here are the relevant code snippets from my two files that I\'m working with:

8条回答
  •  半阙折子戏
    2020-11-28 06:03

    Here is a very simple way programmatically:

    private void setColorStateList(TextView view) {
            int[][] states = new int[][] {
                    new int[] { android.R.attr.state_pressed}, // pressed
                    new int[] { android.R.attr.state_focused}, // focused
                    new int[] { android.R.attr.state_enabled}  // enabled
            };
    
            int[] colors = new int[] {
                    getResources().getColor(R.color.blue),
                    getResources().getColor(R.color.green),
                    getResources().getColor(R.color.green) 
            };
    
            ColorStateList list = new ColorStateList(states, colors);
            view.setTextColor(list);
            view.setClickable(true);
            view.setFocusableInTouchMode(true);
        }
    

提交回复
热议问题