Java 如何使用按钮更改背景色?
如何在Android studio中通过按按钮随机更改背景颜色 这是我的密码:Java 如何使用按钮更改背景色?,java,android,android-studio,Java,Android,Android Studio,如何在Android studio中通过按按钮随机更改背景颜色 这是我的密码: public class partymodus extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_partymodus); final TextVi
public class partymodus extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partymodus);
final TextView aufgabe=(TextView)findViewById(R.id.txt_aufgabe);
final Button next = (Button)findViewById(R.id.btn_next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String[] aufgaben = getResources().getStringArray(R.array.name);
Random rand = new Random();
int n = rand.nextInt(aufgaben.length - 0) + 0;
aufgabe.setText(aufgaben[n]);
}
});
}
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mHomeLl.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
});
您必须在按钮中添加一个
ActionListener
,并覆盖该方法:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
对不起,我的英语是初学者您必须在按钮中添加一个
ActionListener
,并覆盖该方法:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
对不起,我的英语是初学者作为一个联合国专业人士,要回答你的问题,你必须在正式API开始之前挖掘出jave图形设计模式是什么, 以下是搜索JFrame或Java.awt.Graphic的链接
这不是您开始在图形上构建组件的地方,因为您是使用按钮组件构建的,我建议您将JFrame的继承或父界面以及另一个容器作为一个联合国职业来回答您的问题,在进入官方API之前,你必须挖掘出jave图形设计模式是什么,
MyActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyActivity.setBackgroundColor(getResources().getColor(R.color.RED));
}
以下是搜索JFrame或Java.awt.Graphic的链接
它不是您开始在图形上构建组件的地方,因为您是使用按钮组件构建的,我建议您查看JFrame的继承或父界面,以及另一个容器添加
视图_name.setBackgroundColor(getResources().getColor(R.color.colorAccent))代码>
MyActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyActivity.setBackgroundColor(getResources().getColor(R.color.RED));
}
行设置背景色
这是我的代码:
public class partymodus extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partymodus);
final TextView aufgabe=(TextView)findViewById(R.id.txt_aufgabe);
final Button next = (Button)findViewById(R.id.btn_next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String[] aufgaben = getResources().getStringArray(R.array.name);
Random rand = new Random();
int n = rand.nextInt(aufgaben.length - 0) + 0;
aufgabe.setText(aufgaben[n]);
}
});
}
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mHomeLl.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
});
添加view\u name.setBackgroundColor(getResources().getColor(R.color.colorAccent))代码>
行设置背景色
这是我的代码:
public class partymodus extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partymodus);
final TextView aufgabe=(TextView)findViewById(R.id.txt_aufgabe);
final Button next = (Button)findViewById(R.id.btn_next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String[] aufgaben = getResources().getStringArray(R.array.name);
Random rand = new Random();
int n = rand.nextInt(aufgaben.length - 0) + 0;
aufgabe.setText(aufgaben[n]);
}
});
}
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mHomeLl.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
});
谷歌回答了同样的问题:谷歌的可能重复回答了同样的问题:谷歌的可能重复