activity是UI界面,但是根据MVC的原则,有时,我们需要对数据逻辑的处理后,再来更新UI界面,下面这个Demo,提供了三种方式来在数据逻辑处理后更新UI界面。
1.MainActivity.java
package com.example.testviewupdate;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
public static final boolean isDebug = true;
public static final String TAG = "MAINACTIVITY_HXM";
public static final int MES_WHAT_1 = 1;
public static final int MES_WHAT_2 = 2;
private MainActivityHandler myMainActivityHandler = null;
private TextView tv_1 = null;
private TextView tv_2 = null;
private TextView tv_3 = null;
private TextView tv_4 = null;
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MES_WHAT_1:
onUpdateMesWhat1();
break;
case MES_WHAT_2:
onUpdateMesWhat2();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
tv_1 = (TextView)findViewById(R.id.tv_1);
tv_2 = (TextView)findViewById(R.id.tv_2);
tv_3 = (TextView)findViewById(R.id.tv_3);
tv_4 = (TextView)findViewById(R.id.tv_4);
myMainActivityHandler = new MainActivityHandler(mHandler,this);
}
protected void onUpdateMesWhat1() {
// TODO Auto-generated method stub
tv_1.setText(R.string.mHandler_onUpdateMesWhat1);
}
protected void onUpdateMesWhat2() {
// TODO Auto-generated method stub
tv_1.setText(R.string.mHandler_onUpdateMesWhat2);
}
protected void onUpdateTV_2(){
tv_2.setText(R.string.RunOnUiThread);
}
protected void onUpdateTV_3(){
tv_3.setText(R.string.onUpdateTV_3);
}
}
2.MainActivityHandler.java
package com.example.testviewupdate;
import android.os.Handler;
import android.util.Log;
public class MainActivityHandler{
private Handler mHandler = null;
private MainActivity activity = null;
private boolean flag = true;
private int mes_what = 0;
public MainActivityHandler(Handler mHandler,MainActivity activity) {
// TODO Auto-generated constructor stub
this.mHandler = mHandler;
this.activity = activity;
startThreadForHandler();
startThreadForRunOnUiThread();
updateViewForDirecCallFunc();
}
private void updateViewForDirecCallFunc() {
// TODO Auto-generated method stub
activity.onUpdateTV_3();
}
private void startThreadForRunOnUiThread() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
activity.onUpdateTV_2();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void startThreadForHandler() {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(flag = true){
try {
Thread.sleep(1000);
mes_what++;
if(mes_what > 2)
mes_what = 1;
mHandler.sendEmptyMessage(mes_what);
if(MainActivity.isDebug){
Log.i(MainActivity.TAG,"mHandler.sendEmptyMessage(mes_what):"+mes_what);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}
3.res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv_2"
android:layout_below="@id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_strings_1" />
<TextView
android:id="@+id/tv_3"
android:layout_below="@id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_strings_2" />
<TextView
android:id="@+id/tv_4"
android:layout_below="@id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_strings_3" />
</RelativeLayout>