`
shenjichao2009
  • 浏览: 94439 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android中Intent和ProgressBar的结合使用

阅读更多

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">IntentProgressBar</string>
 <string name="start">开始</string>
</resources>

main.xml

<?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/progressBarId"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  style="@android:style/Widget.ProgressBar.Horizontal" android:max="200"
  android:visibility="gone" />

 <Button android:id="@+id/startButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="@string/start" />

</LinearLayout>

 

IntentProgressBarActivity.java

package com.duoguo.android;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

/**
 * Intent在ProgressBar中的使用
 *
 * @author shyboy(
897948924@qq.com)
 *
 */
public class IntentProgressBarActivity extends Activity {

 private ProgressBar progressBar;// 声明ProgressBar控件
 private Button startButton;// 声明Button控件

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  progressBar = (ProgressBar) findViewById(R.id.progressBarId);// 根据控件id获取ProgressBar控件对象
  startButton = (Button) findViewById(R.id.startButtonId);// 根据控件id获取Button控件对象
  startButton.setOnClickListener(new StartButtonClickListener());// 为startButton按钮控件添加单击事件监听器

 }

 // 声明startButton控件的事件监听器
 class StartButtonClickListener implements OnClickListener {

  @Override
  public void onClick(View v) {

   progressBar.setVisibility(View.VISIBLE);// 设置progressBar进度条为可见
   handler.post(runnable);// 将线程添加到线程队列中

  }

 }

 // 实例化Handler对象,通过handleMessage用来接收队列中的消息
 Handler handler = new Handler() {

  @Override
  public void handleMessage(Message msg) {

   progressBar.setProgress(msg.arg1);// 设置progressBar进度条的第一进度值
   handler.post(runnable);// 将线程添加到线程队列当中

  }

 };

 // 实例化线程runnable对象
 Runnable runnable = new Runnable() {

  int i = 0;

  @Override
  public void run() {

   System.out.println("thread start");
   i += 10;

   Message message = handler.obtainMessage();// 获取消息对象
   message.arg1 = i;// 用成员变量arg1来存放i的值

   try {

    Thread.sleep(1000);// 线程休眠1s

    handler.sendMessage(message);// 将消息添加到消息队列的尾端

    if (i == progressBar.getMax()) {
     handler.removeCallbacks(runnable);// 将线程对象从消息队列中移除
     progressBar.setVisibility(View.GONE);
     i = 0;
    }

   } catch (InterruptedException e) {

    e.printStackTrace();

   }

  }
 };

}

 

希望对你有所帮助,呵呵……

0
1
分享到:
评论

相关推荐

    《Google Android开发入门与实战》.pdf

    7.5 android应用的灵魂——intent和activity介绍与实例 106 7.5.1 实例操作演示 106 7.5.2 实例编程实现 106 7.6 用好列表,做好程序——列表(listview)介绍与实例 111 7.6.1 实例程序演示 111 7.6.2...

    Google.Android开发入门与实战

     由于Android平台被推出的时间才一年左右,了解Android平台软件开发技术的程序员还不多,如何迅速地推广和普及Android平台软件开发技术,让越来越多的人参与到Android应用的开发中,是整个产业链都在关注的一个话题...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK开发范例大全的目录

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信...

    Google Android SDK 开发范例大全01

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK 开发范例大全02

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google+Android+SDK开发范例大全

    1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 安装AndroidSDK与ADTplug-in 2.2 建立第一个Android项目(HelloAndroid!...

    Google Android sdk 开发范例大全 部分章节代码

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

    Google Android SDK开发范例大全(完整版)

    6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送...

Global site tag (gtag.js) - Google Analytics