lundi 28 novembre 2016

Display a Shuffle of button text and backgrounds

I have a button in xml. On button press I wish to rapidly change the background and Text on that button.

I normally would use a code like this for the final result:

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"));}

Before that is called though I would like perhaps a second or two of a "shuffling" effect.

I have tried using java.util.timer like this:

@Override
            public void onClick(View v) {                           

                    new java.util.Timer().schedule(
                        new java.util.TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new       Runnable() 
                                        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 );

Then making a few of these with different backgrounds to fire one after the other. I just can't seem to get the hang of it. From what I have read on here and in the Oracle docs, I understand you need to use EventQueue.invokeLater To refresh the GUI, but I can't seem to get a clear use.

I may need a whole new method to do what I want to do, but I am not sure what the best wat to accomplish what I need is.




Aucun commentaire:

Enregistrer un commentaire