简体   繁体   中英

Java:How to Randomly Change Background And Font Color

在此处输入图像描述

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.

Set on onClickListener for whatever you want to be clicked in order for the colors to change (you could create a view that height and width is match_parent, and overlay it over other elements:

view.setOnClickListener(v - > {
    //Text
    textView.setTextColor(Color.RED);

    //Background
    image.setImageResource(R.drawable.different_color_background);
}

There's two thing.

  1. onCLickListener .
  2. onTouchListener .

onClickListener works when you click on a thing / view . onTouchListener works when you touch or, hover the thing / view .

I am going to give you both example.

view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Text
            textView.setTextColor(Color.RED);

            //Background
            image.setImageResource(R.drawable.different_color_background);
        }
    });
    


view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Text
            textView.setTextColor(Color.RED);

            //Background
            image.setImageResource(R.drawable.different_color_background);
            return false;
        }
    });

But, your title is saying to change background randomly. So, you have to create an array(drawable array/drawable list). You can take color array or color list. Then,

// create instance of Random class 
    Random rand = new Random(); 

    // Generate random integers in range 0 to 999 
    int rand_int1 = rand.nextInt(list.length); 

take random number. Show that color instead of color or image resource

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]);
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM