首页 > 代码库 > Hander----使用

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 . 子线程发消息                      }      }  }

  

Hander----使用