如何将我的按钮设置为背景的随机颜色。 Android
How can set my button a random color to the background. Android
抱歉我的英语不好我是 android 的初学者,现在我被卡住了。
现在我的问题是如何使用 OnClickListener
将随机颜色设置为背景。你能帮我解决这个问题吗?
我有一个 class (Kleurenpalet.java)
package com.example.pstek.randomcolor;
import android.graphics.Color;
import java.util.Random;
public class Kleurenpalet{
private static String[] kleur = {
"#39add1", // light blue
"#3079ab", // dark blue
"#3FD52D", // green
"FFFF0000", // red
""};
public int getRandomColor() {
Random rand = new Random();
int color = rand.nextInt(kleur.length);
return Color.parseColor(kleur[color]);
}
}
我有我的主要 class :
package com.example.pstek.tegeltjeswijsheid;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.solver.widgets.ConstraintWidget;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
int randomColor = new Kleurenpalet().getRandomColor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
}
});
}
}
也许用初始化颜色调用setBackgroundColor
:
layout.setBackgroundColor(randomColor);
或每次不同:
layout.setBackgroundColor(new Kleurenpalet().getRandomColor());
我不明白你想在这里做什么:
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
你为什么把 space 留在那里?此外,如果您在 Kleurenpalet
中解析颜色,则必须不使用 ContextCompat
。只需像这样设置颜色:
layout.setBackgroundColor(randomColor);
ContextCompat
是从资源文件解析颜色,例如:
layout.setBackgroundColor(
ContextCompat.getColor(
getApplicationContext(),
R.color.colorPrimary
)
);
在 MainActivity 代码中:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
}
}
您必须将 "Random Num" 和 "layout set Color" 放入按钮点击事件中:
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
抱歉我的英语不好我是 android 的初学者,现在我被卡住了。
现在我的问题是如何使用 OnClickListener
将随机颜色设置为背景。你能帮我解决这个问题吗?
我有一个 class (Kleurenpalet.java)
package com.example.pstek.randomcolor;
import android.graphics.Color;
import java.util.Random;
public class Kleurenpalet{
private static String[] kleur = {
"#39add1", // light blue
"#3079ab", // dark blue
"#3FD52D", // green
"FFFF0000", // red
""};
public int getRandomColor() {
Random rand = new Random();
int color = rand.nextInt(kleur.length);
return Color.parseColor(kleur[color]);
}
}
我有我的主要 class :
package com.example.pstek.tegeltjeswijsheid;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.solver.widgets.ConstraintWidget;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
int randomColor = new Kleurenpalet().getRandomColor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
}
});
}
}
也许用初始化颜色调用setBackgroundColor
:
layout.setBackgroundColor(randomColor);
或每次不同:
layout.setBackgroundColor(new Kleurenpalet().getRandomColor());
我不明白你想在这里做什么:
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
你为什么把 space 留在那里?此外,如果您在 Kleurenpalet
中解析颜色,则必须不使用 ContextCompat
。只需像这样设置颜色:
layout.setBackgroundColor(randomColor);
ContextCompat
是从资源文件解析颜色,例如:
layout.setBackgroundColor(
ContextCompat.getColor(
getApplicationContext(),
R.color.colorPrimary
)
);
在 MainActivity 代码中:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
}
}
您必须将 "Random Num" 和 "layout set Color" 放入按钮点击事件中:
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});