gpt4 book ai didi

java - 执行每秒更新 UI 的 runnable 时出现问题

转载 作者:太空狗 更新时间:2023-10-29 14:08:47 26 4
gpt4 key购买 nike

我正在尝试在 Activity 运行时每秒更新一次按钮的背景。一切似乎都适用于 runnable,因为 Logcat 在这两行中每秒输出整数:Log.d("randomint", Integer.toString(randomInt));Log .d("back", Integer.toString(back)); 但是我在屏幕上看不到任何视觉变化,因为所有按钮的背景都保持不变。

public class CoinFrenzy extends Activity {

private int draw1 = R.drawable.buttonshape1;
private int draw2 = R.drawable.buttonshape2;
private int draw3 = R.drawable.buttonshape3;
private int draw4 = R.drawable.buttonshape4;
private int draw5 = R.drawable.buttonshape5;
private int draw6 = R.drawable.buttonshape6;
private int draw7 = R.drawable.buttonshape7;
private Handler h = new Handler(Looper.getMainLooper());
private int draw8 = R.drawable.buttonshape8;
private ArrayList<Integer> selector = new ArrayList<>();
private ArrayList<Button> buttonlist = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coin_frenzy);

h.postDelayed(myRunnable, 1000);


}

public static int randInt(int min, int max) {

// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;

return randomNum;
}


private Runnable myRunnable = new Runnable() {
public void run() {
Button btn1 = (Button) findViewById(R.id.angry_btn);
Button btn2 = (Button) findViewById(R.id.angry_btn2);
Button btn3 = (Button) findViewById(R.id.angry_btn3);
Button btn4 = (Button) findViewById(R.id.angry_btn4);
Button btn5 = (Button) findViewById(R.id.angry_btn5);
Button btn6 = (Button) findViewById(R.id.angry_btn6);
Button btn7 = (Button) findViewById(R.id.angry_btn7);
Button btn8 = (Button) findViewById(R.id.angry_btn8);
buttonlist.add(btn1);
buttonlist.add(btn2);
buttonlist.add(btn3);
buttonlist.add(btn4);
buttonlist.add(btn5);
buttonlist.add(btn6);
buttonlist.add(btn7);
buttonlist.add(btn8);
selector.add(draw1);
selector.add(draw2);
selector.add(draw3);
selector.add(draw4);
selector.add(draw5);
selector.add(draw6);
selector.add(draw7);
selector.add(draw8);
for (Integer x : selector) {
int randomInt = randInt(0, 7);
Log.d("randomint", Integer.toString(randomInt));
int back = selector.get(randomInt);
Log.d("back", Integer.toString(back));
Button currbtn = buttonlist.get(randomInt);
currbtn.setBackgroundResource(back);

}
h.postDelayed(myRunnable, 1000);

//run again in one second
}

};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_coin_frenzy, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


}

最佳答案

我认为这是因为您如何使用随机数来获取所需图像的索引和所需的按钮 - 导致它们每次都映射到相同的项目。您应该只遍历按钮列表,为背景生成一个随机索引,然后将其设置在当前迭代的按钮上。

关于java - 执行每秒更新 UI 的 runnable 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738722/

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