我的示例Android应用中有两个按钮。我想随机选择一个按钮并将其背景图像(变为黄色)更改并显示4秒钟。再过4秒后,我想要将背景图像(变为蓝色)更改并显示4秒钟。现在重复按钮随机选择的过程,并使用如上所述的根本选择的按钮执行相同操作。
我开发了一些代码,当我测试indvidual按钮的代码时,它运行正常,但是当我为两个按钮运行时,它都不能正常工作。
请帮助我,我将非常感谢你。您可以将我的代码检查为wl ....
int mainCount =0;// mainCount- could be a random number
int count_1 =0;
int count_2 =0;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mlayout);
mainHandler = new Handler();
mHandler1 = new Handler();
mHandler2 = new Handler();
.
.
.
mainRunnable.run();
}
Runnable mainRunnable = new Runnable(){
public void run(){
mainHandler.postDelayed(new Runnable(){
public void run(){
switch(mainCount){
case 0:
runButton1.run();
mainCount++; // mainCount- could be a random number to select a button randomly
break;
case 1:
runButton2.run();
mainCount++;// mainCount- could be a random number to select a button randomly
break;
}
if(count==2)
mainCount =0;
mainHandler.postDelayed(this,4000);
}
}, 4000);
}
};
Runnable runButton1 =new Runnable(){
public void run(){
mHandler1.postDelayed(new Runnable(){
public void run(){
switch(count_1){
case 0:
button1.setBackgroundResource(R.drawable.buttonyellow);
count_1++;
break;
case 1:
button1.setBackgroundResource(R.drawable.buttonblue);
count_1++;
break;
}
if(count_1==2)
count_1 = 0;
mHandler1.postDelayed(this,4000);
}
}, 4000);
}
};
Runnable runButton2 =new Runnable(){
public void run(){
mHandler2.postDelayed(new Runnable(){
public void run(){
switch(count_2){
case 0:
button2.setBackgroundResource(R.drawable.buttonyellow);
count_2++;
break;
case 1:
button2.setBackgroundResource(R.drawable.buttonblue);
count_2++;
break;
}
if(count_2==2)
count_2 = 0;
mHandler2.postDelayed(this,4000);
}
}, 4000);
}
};
答案 0 :(得分:1)
首先,你不需要多个Handler
,一个就够了。其次,你在mainRunnable.run()
中调用onCreate
来运行另一个内部runnable,这样才更适合作为一个方法。无论如何,这是我的看法:
public class MyActivity extends Activity {
private Runnable mEndlessRunnable;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.my_activity);
mEndlessRunnable = new UpdateRunnable(new Handler(), new Button[] {
(Button) findViewById(R.id.button_1),
(Button) findViewById(R.id.button_2)
});
mEndlessRunnable.run();
}
private static class UpdateRunnable extends Runnable {
private Random mRand = new Random();
private Handler mHandler;
private Button[] mButtons;
private Button mCurButton;
private int mState;
public UpdateRunnable(Handler handler, Button[] buttons) {
mHandler = handler;
mButtons = buttons;
}
public void run() {
// select a button if one is not selected
if (mCurButton == null) {
mCurButton = mButtons[mRand.nextInt(mButtons.length)];
}
// check internal state, `0` means first bg change, `1` means last
switch (mState) {
case 0:
mCurButton.setBackgroundResource(R.drawable.blue_bg);
mState = 1;
break;
case 1:
mCurButton.setBackgroundResource(R.drawable.yellow_bg);
// reset state and nullify so this continues endlessly
mState = 0;
mCurButton = null;
break;
}
mHandler.postDelayed(this, 4000);
}
}
}
我没有测试过上面的内容,但我会将上面的内容用作参考。享受