[英]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:在 onClickListener 上设置您想要单击的任何内容,以便 colors 更改(您可以创建一个高度和宽度为 match_parent 的视图,并将其覆盖在其他元素上:
view.setOnClickListener(v - > {
//Text
textView.setTextColor(Color.RED);
//Background
image.setImageResource(R.drawable.different_color_background);
}
There's two thing.有两件事。
onCLickListener
. onCLickListener
。onTouchListener
. onTouchListener
。 onClickListener
works when you click on a thing
/ view
. onClickListener
在您单击thing
/ view
时起作用。 onTouchListener
works when you touch or, hover the thing
/ view
. onTouchListener
在您触摸或 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
显示该颜色而不是
color
或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;
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]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.