Android开发之UI更新交互机制与实例解析

android开发过程中,经常需要更新UI的状态和文案等。这是就需要对UI进行 更新。在android中更新UI一般有三种方法,handler机制、RunOnUiThread方法以及AsyncTask异步类方法等

本文下面就这三种方法进行了演示和代码实现.

a.Handler机制通过使用消息机制来实现

b.RunOnUiThread方法是通过运行UI线程来达到更新UI的目的

c.AsyncTask是异步类,通过异步更新来更新UI

效果图如下:

Android开发之UI更新交互机制与实例解析    Android开发之UI更新交互机制与实例解析

 

 

Android开发之UI更新交互机制与实例解析    Android开发之UI更新交互机制与实例解析

 

(1)Java功能实现代码如下:

package com.czm.updateui;



import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.R.integer;

import android.app.Activity;

import android.graphics.Color;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.TextView;



public class UpdateUIActivity extends Activity {



    

    private Button btnHandler;

    private Button btnRunOnUiThread;

    private Button btnAsyncTask;

    private TextView tvHandler;

    private TextView tvOnUiThread;

    private TextView tvProgress;

    private Handler uiHandler;

    private ProgressBar progressBar;

    

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.update_ui);

        initViews();

        setListeners();

    }

    private void initViews(){

        btnHandler = (Button) findViewById(R.id.btnHandler);

        btnRunOnUiThread = (Button) findViewById(R.id.btnRunOnUiThread);

        btnAsyncTask = (Button)findViewById(R.id.btnAsyncTask);

        tvHandler = (TextView) findViewById(R.id.tvText1);

        tvOnUiThread = (TextView)findViewById(R.id.tvText2);

        tvProgress = (TextView)findViewById(R.id.tvText3);

        progressBar = (ProgressBar)findViewById(R.id.progressBar);

    }

    private void setListeners(){

        uiHandler = new Handler(){



            @Override

            public void handleMessage(Message msg) {

                // TODO Auto-generated method stub

                super.handleMessage(msg);

                Bundle bundle = msg.getData();

                String text = bundle.getString("handler_text");

                String color = bundle.getString("handler_color");

                

                tvHandler.setText(text);

                tvHandler.setBackgroundColor(Color.BLUE);

                

            }

            

        };

        //通过Handler机制来更新UI

        btnHandler.setOnClickListener(new OnClickListener() {

            

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                new Thread(){



                    @Override

                    public void run() {

                        // TODO Auto-generated method stub

                        Message msg =new Message();

                        Bundle bundle = new Bundle();

                        bundle.putString("handler_text", "我是由Handler更新UI后的文案");

                        bundle.putString("handler_color", "#0000FF");

                        msg.setData(bundle);

                        //uiHandler.sendMessageDelayed(msg, 2000);

                        uiHandler.sendMessage(msg);

                    }

                    

                }.start();

            }

        });

        

        //通过RunOnUiThread来更新UI

        btnRunOnUiThread.setOnClickListener(new OnClickListener() {

            

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                

                runOnUiThread(new Runnable() {

                    

                    @Override

                    public void run() {

                        // TODO Auto-generated method stub

                        tvOnUiThread.setText("我是由runOnUiThread更新UI后的文案");

                        tvOnUiThread.setBackgroundColor(Color.RED);

                        

                    }

                });

            }

        });

        //通过AsyncTask 异步任务来更新UI

        btnAsyncTask.setOnClickListener(new OnClickListener() {

            

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                new MyAsyncTask().execute(0);

            }

        });

    }



    private class MyAsyncTask extends AsyncTask<Integer, Integer, Integer>{



        @Override

        protected void onPostExecute(Integer result) {

            // TODO Auto-generated method stub

            super.onPostExecute(result);

            tvProgress.setText("加载完成...100%");

        }



        @Override

        protected void onProgressUpdate(Integer... values) {

            // TODO Auto-generated method stub

            super.onProgressUpdate(values);

            progressBar.setProgress((int)(values[0]));

            tvProgress.setText("加载中..."+values[0]+"%");

        }



        @Override

        protected Integer doInBackground(Integer... params) {

            // TODO Auto-generated method stub

            Integer timer = 0;

            while(timer <=100){

                try {

                    publishProgress(timer);

                    timer ++;

                    Thread.sleep(40);

                } catch (InterruptedException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

            return null;

        }

         

    }

    



}

(2)对应的UI布局xml文件代码:

<LinearLayout 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:orientation="vertical"

    android:gravity="center_horizontal"

    tools:context=".UpdateUIActivity" >

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >"

        <Button

        android:id="@+id/btnHandler"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Handler" />

    <Button

        android:id="@+id/btnRunOnUiThread"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="runOnUiThread" />

    <Button

        android:id="@+id/btnAsyncTask"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="AsyncTask" />

    </LinearLayout>

    

    <TextView

        android:id="@+id/tvText1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:text="Handler来更新UI"

        android:padding="3dp"

        android:textColor="#FFF"

        android:background="#666666"

        android:textSize="20dp" />

    <TextView

        android:id="@+id/tvText2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:text="RunOnUiThread来更新UI"

        android:padding="3dp"

        android:textColor="#FFF"

        android:background="#666666"

        android:textSize="20dp" />

    

    <ProgressBar android:id="@+id/progressBar"

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="fill_parent"

        android:layout_height="25dp"

        android:progressDrawable="@drawable/progressbar"

        android:max="100"

        android:progress="0"

        android:layout_marginLeft="5dp"

        android:layout_marginRight="5dp"

        android:layout_marginTop="10dp"

         />

    <TextView

        android:id="@+id/tvText3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="准备加载...0%"

        android:textSize="20dp" />

    

    



</LinearLayout>

 

你可能感兴趣的:(Android开发)