首页 > 移动平台 > 详细

Android-Handler更新View

时间:2014-12-28 16:46:18      阅读:373      评论:0      收藏:0      [点我收藏+]

技术分享

代码

package net.blogjava.mobile;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class Main extends Activity
{
    
    private ProgressBar progressBar;

    private Handler handler = new Handler()
    {

        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 1:
                    int currentProgress = progressBar.getProgress() + 2;
                    if (currentProgress > progressBar.getMax())
                        currentProgress = 0;
                    progressBar.setProgress(currentProgress);
                    break;
            }
            super.handleMessage(msg);
        }

    };
    private TimerTask timerTask = new TimerTask()
    {

        public void run()
        {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        Timer timer = new Timer();
        timer.schedule(timerTask, 0, 500);



    }
}

 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ProgressBar android:id="@+id/progressbar"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_marginTop="20dp" android:max="100"
        style="?android:attr/progressBarStyleHorizontal" />
</LinearLayout>

 

Android-Handler更新View

原文:http://www.cnblogs.com/spadd/p/4190034.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!