简体   繁体   English

每秒更改按钮的背景

[英]Changing background of button every second

I have an activity with 8 buttons in it. 我有一个带有8个按钮的活动。 I want the backgrounds for all the buttons to randomly change between each other every second that the activity is running. 我希望所有按钮的背景在活动运行的每一秒之间随机地变化。 To do this I use the following code: 为此,我使用以下代码:

 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 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); 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); 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 Handler h = new Handler(Looper.getMainLooper()); private Runnable myRunnable = new Runnable() { public void run() { for (Integer x : selector) { int randomInt = randInt(0, 7); int back = selector.get(randomInt); Button currbtn = buttonlist.get(randomInt); currbtn.setBackgroundResource(back); } //run again in one second h.postDelayed(myRunnable, 1000); }}; 

However, after one second passes my app crashes without any logcat errors. 但是,一秒钟过后,我的应用程序崩溃了,没有任何logcat错误。

First do these 2 things: 首先执行以下两件事:

  1. You have to remove change the background of the button code from thread. 您必须从线程中删除更改按钮代码的背景。 This is the main cause of crash. 这是崩溃的主要原因。 Below you can find two methods on how can one run a code on the main/UI thread on android: 在下面,您可以找到两种方法来解决如何在android的main / UI线程上运行代码的问题:
  2. runOnUIThread Activity's method runOnUIThread活动的方法

     runOnUiThread(new Runnable() { public void run() { // change background color here } }); 
  3. Handler 处理程序

     Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { // UI code goes here } }); 

you might want to use one of the two methods whenever you want to update the UI like updating colors on views or other UI related stuff. 每当您要更新UI(例如更新视图或其他与UI相关的东西的颜色)时,您可能都想使用两种方法之一。 Otherwise, the application might crash because the UI classes on Android are not thread safe. 否则,应用程序可能会崩溃,因为Android上的UI类不是线程安全的。

This will help you!! 这会帮助你!

Replace your 更换您的

private Handler h = new Handler();

to

private Handler h = new Handler(Looper.getMainLooper());

otherwise your code will be run outside the UI thread 否则,您的代码将在UI线程外运行

You have to pass dynamic index into 您必须将动态索引传递到

int randomInt = randInt(0, 7);

you are always passing same values for min is 0 and max is 7, so it doesn't change any background. 您总是传递相同的值,因为min为0,max为7,因此不会更改任何背景。

Use selector random ids replace 7 with dynamic loop value x. 使用选择器随机ID将7替换为动态循环值x。

private Runnable myRunnable = new Runnable() {
        public void run() {
            for (Integer x : selector) {
                int randomInt = randInt(0, x);
                int back = selector.get(randomInt);
                Button currbtn = buttonlist.get(randomInt);
                currbtn.setBackgroundResource(back);

            }

            //run again in one second
                h.postDelayed(myRunnable, 1000);

        }};

It would be helpful to you. 这将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM