Android Handler Periodically

前端 未结 3 741
清歌不尽
清歌不尽 2021-01-12 15:22

This I want I want to achieve:

  1. An activity starts with no ClickListener and has four textviews all with white background

  2. I want to change th

3条回答
  •  梦谈多话
    2021-01-12 15:35

    I have one sample for this task but by thread with handleMessage

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.Handler;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class l15_threadOneaacto extends Activity  {
        /** Called when the activity is first created. */
        TextView tv[]=new TextView[4];
        EditText et;
        Thread bcko;
        static int index=0;
    
        boolean isRunning=false;
        boolean acceptevent=false;
        Handler hn=new Handler(){
            @Override
            public void handleMessage(android.os.Message msg) {
                switch (index) {
                case 0: tv[0].setBackgroundColor(Color.BLUE);  break;
                case 1: tv[0].setBackgroundColor(Color.WHITE);  break;
                case 2: tv[1].setBackgroundColor(Color.BLUE);  break;
                case 3: tv[1].setBackgroundColor(Color.WHITE);  break;
                case 4: tv[2].setBackgroundColor(Color.BLUE);  break;
                case 5: tv[2].setBackgroundColor(Color.WHITE);  break;
                case 6: tv[3].setBackgroundColor(Color.BLUE);  break;
                case 7: tv[3].setBackgroundColor(Color.WHITE);  break;
                }
                index++;
                if(index==8){
                    acceptevent=true;
                    et=(EditText)findViewById(R.id.bbb);
                    et.setText("ready for input");
                }
    
            };
    
        };
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            tv[0]=(TextView)findViewById(R.id.tx1);
            tv[1]=(TextView)findViewById(R.id.tx2);
            tv[2]=(TextView)findViewById(R.id.tx3);
            tv[3]=(TextView)findViewById(R.id.tx4);
    
    
    
    
    
        }
    
    
    
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
    
             bcko=new Thread (new Runnable() {
    
                @Override
                public void run() {
                    try {
    
                            //
                            for(int i=0;i<8 && isRunning;i++){
                                Thread.sleep(2000);
                            hn.sendMessage(hn.obtainMessage());
                            }
    
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
    
                }
            });
            isRunning=true;
            bcko.start();
    
    
    
        }
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            isRunning=false;
        }
    }
    

    kayout:

    
    
    
        
        
        
        
    
    

    Good luck,

提交回复
热议问题