我在xml中有一个按钮。按下按钮时,我希望快速更改该按钮上的背景和文字。显示按键文本和背景的随机数
我通常会使用这样的代码,最终的结果:
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
{Button btn = (Button) findViewById(R.id.button1);
btn.setText(String.valueOf(rnd));
btn.setTextColor(Color.parseColor("#000000"));}
在此之前,被称为虽然我想也许是一个“洗牌”效应两秒钟。
我一直在使用java.util.Timer中这样的尝试:
@Override
public void onClick(View v) {
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
{Button btn = (Button) findViewById(R.id.button1);
btn.setText(String.valueOf(rnd));
btn.setTextColor(Color.parseColor("#000000"));}
}}}, 100);
然后让其中的一些具有不同背景的火一前一后。我似乎无法得到它的窍门。
我可能需要一个全新的方法来做我想做的事,但我不确定什么是最好的wat来完成我所需要的。
'EventQueue'是一部分awt,它不是Android SDK的一部分...你已经使用了带有空Runnable实现的runOnUiThread ... – Selvin
那么这就是为什么当我阅读使用它的时候我非常困惑:) – Oliver
我已经编辑它保持巨魔在海湾。那么,你碰巧知道如何完成我所需要的? – Oliver