0

我正在开发一个简单的应用程序,其中按钮背景在一秒钟后动态变化。我的应用程序名称中有一个按钮 stopButtonBackgroundChanging,当我单击该按钮时,它会停止更改按钮背景,但 2 秒后应用程序意外停止”。请在这方面帮助我,并查看下面的代码,您可能会得到更好的主意这才是我真正想做的。

UpdateRunnable updateRunnable;  
private Runnable mEndlessRunnable;


     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.myLayout);


            Button stopButtonBackgroundChanging = (Button)findViewById(R.id.buttonStop);
            stopButtonBackgroundChanging.setOnClickListener(new OnClickListener() 
            {

                public void onClick(View v) 
                {
                    //See implementation of stop() method at the end of the code
                    updateRunnable.stop();

                }
            });


            mEndlessRunnable = (Runnable) new UpdateRunnable(new Handler(), new Button[] 
            {

                    (Button) findViewById(R.id.button1),
                    (Button) findViewById(R.id.button2),
                    (Button) findViewById(R.id.button3),
                    (Button) findViewById(R.id.button4),

            });
            mEndlessRunnable.run();






        }// End of onCreate()



     //Start of UpdateRunnable
     private static class UpdateRunnable implements Runnable 
     {

         private boolean myContinue = true;


            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() 
            {
                if(myContinue)
                {
                    // 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.buttonyellow);
                        mState = 1;
                        break;
                    case 1:
                        mCurButton.setBackgroundResource(R.drawable.buttonblue);
                        // reset state and nullify so this continues endlessly
                        mState = 0;
                        mCurButton = null;
                        break;
                    }

                    mHandler.postDelayed(this, 1000); 

                }




            }// End of run()

            public void stop()
            {
             this.myContinue =false;    
            }

        }//End of class UpdateRunnable
4

1 回答 1

2

我相信您可以通过更改 onClick 来实现此功能:

public void onClick(View v)
{
    //See implementation of stop() method at the end of the code
    ((UpdateRunnable) mEndlessRunnable).stop();
}
于 2012-10-07T16:35:46.610 回答