gpt4 book ai didi

java - 如何用按钮更改背景颜色?

转载 作者:行者123 更新时间:2023-12-02 01:39:41 25 4
gpt4 key购买 nike

如何在 Android studio 中按下按钮随机更改背景颜色?

这是我的代码:

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

}
});
}

最佳答案

您必须向按钮添加一个 ActionListener 并重写该方法:

public void actionPerformed(ActionEvent e) { 
...//code that reacts to the action...
}

对不起我的英语,我是初学者

关于java - 如何用按钮更改背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54622265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com