zoukankan      html  css  js  c++  java
  • Hander----使用

    public class MainActivity extends Activity {  
        private EditText UITxt;  
        private Button updateUIBtn;  
        private UIHandler uIhandler;  
      
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            UITxt = (EditText)findViewById(R.id.ui_txt);  
            updateUIBtn = (Button)findViewById(R.id.update_ui_btn);  
            updateUIBtn.setOnClickListener(new View.OnClickListener() {  
                  
                public void onClick(View v) {  
             
                    uIhandler= new UIHandler();    //  创建Handler 对象
                    UIThread thread = new UIThread();  
                    thread.start();   // 
                }  
            });  
        }  
    
    
        private class UIHandler extends Handler{  
            @Override  
            public void handleMessage(Message msg) {    //  2 .主线程更新界面
               
                super.handleMessage(msg);  
    
    
                Bundle bundle = msg.getData();  
                String color = bundle.getString("color");  
                UITxt.setText(color);  
    	//	if(msg.what == SUCCESS) {		// 当前是访问网络, 去显示图片
    	//		ivIcon.setImageBitmap((Bitmap) msg.obj);		// 设置imageView显示的图片
    	//	} else if(msg.what == ERROR) {
    	//		Toast.makeText(MainActivity.this, "抓去失败", 0).show();
    	//	}
    	
    
            }  
        }  
    	//  子线程 中运行
        private class UIThread extends Thread{  
            @Override  
            public void run() {  
                try {  
                    Thread.sleep(3000);  
                } catch (InterruptedException e) {  
                
                    e.printStackTrace();  
                }  
                Message msg = new Message();  
    		// msg.what = SUCCESS;
    		// msg.obj = bitmap; 
                Bundle bundle = new Bundle();  
                bundle.putString("color", "黄色");  
                msg.setData(bundle);  
                MainActivity.this.uIhandler.sendMessage(msg);    //1 . 子线程发消息
                  
            }  
        }  
    }
    

      

  • 相关阅读:
    XJ20夏令营做题记录(长期更新)
    洛谷P6623——[省选联考 2020 A 卷] 树
    [游记] 2020ZJOI 爆零记
    CF1017G——The Tree
    CF715E—— Complete the Permutations
    学习笔记——树的初步整理
    学习笔记——DP初步整理
    洛谷P5290——春节十二响
    POJ3017——Cut the Sequence(单调队列+堆优化DP)
    Java控制整形输入的法子
  • 原文地址:https://www.cnblogs.com/java-g/p/4132259.html
Copyright © 2011-2022 走看看