android开发笔记之activity之数据逻辑分开处理后界面更新

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值