Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

android - Java:How to Randomly Change Background And Font Color

enter image description here

If you touch the screen, I wish the background color and font color were changed together.

I want to change the font and background into a single set and change it to a specified unique color.

It is not difficult to change them separately, but I am not sure how to change the font and background color at the same time.

Waiting for your help. I want to say thank you in advance.

question from:https://stackoverflow.com/questions/65895796/javahow-to-randomly-change-background-and-font-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to change it randomly you can use this one

 private void changeColors () {
        switch (new Random().nextInt([Number Of options])) {
            case 0:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
            case 1:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
            case 2:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
            case 3:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
            case 4:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
            case 5:
                textView.setTextColor([COLOR]);
                layout.setBackgroundColor([COLOR]);
        }

    }

If you want to set them in an order you could use this, setting a counter in the on-click listener defining an n outside of it;

n=0;

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        changeColors(n);
        n + 1;
    }
});

private void changeColors () {
    switch (n) {
        case 0:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
        case 1:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
        case 2:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
        case 3:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
        case 4:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
        case 5:
            textView.setTextColor([COLOR]);
            layout.setBackgroundColor([COLOR]);
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...