我正在编写一个应用程序,每次按下按钮都会更改活动的背景颜色。这是我目前的代码。但它没有起作用!我做错了什么?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
final View a = findViewById(R.id.m);
final Random color = new Random();
final Paint p = new Paint();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
p.setARGB(256,color.nextInt(256),color.nextInt(256),color.nextInt(256));
a.setBackgroundColor((p.getColor()));
}
});
}
当我传递单一颜色时,它可以正常工作,例如 a.setBackgroundColor(Color.GREEN);
Color
类吗?像这样:a.setBackgroundColor(Color.argb(256, color.nextInt(256), color.nextInt(256), color.nextInt(256)));
- Catp.setARGB(256
,应该是p.setARGB(255
。 - Maxime