标签:
1 编写handle和setActivityresult来跟更数据写起来太麻烦,简单的介绍一下eventbus,直接来把,不用讲那么多原理
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_linearlayout); EventBus.getDefault().register(this); new Thread(new Runnable() { @Override public void run() { while (true) { Log.i("===>>", "post"); EventBus.getDefault().post(String.valueOf((int) (Math.random() * 100 + 1))); try { Thread.sleep(1000); } catch (Exception e) { } } } }).start(); }
2 首先在ONcreate 里面注册
@Subscribe(threadMode = ThreadMode.MAIN) public void update(String s) { Log.i("===>>", "接收" + s); ((TextView) findViewById(R.id.tv)).setText(s); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().register(this); }
然后写跟新function。 注意这里要跟新ui 说有 用了 threadmode.main 在ui 主线程中去跟新
3 布局文件为
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="200dp" android:layout_gravity="center" android:layout_centerInParent="true" android:gravity="center" android:background="#ff6600" android:textColor="@color/text1" android:textSize="40dp" android:layout_height="100dp" /> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/seanzhoutao/p/5655894.html