手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android控件之ProgressBar用法实例分析
Android控件之ProgressBar用法实例分析
摘要:本文实例讲述了Android控件之ProgressBar用法。分享给大家供大家参考。具体如下:ProgressBar位于android.wi...

本文实例讲述了Android控件之ProgressBar用法。分享给大家供大家参考。具体如下:

ProgressBar位于android.widget包下,其继承于View,主要用于显示一些操作的进度。应用程序可以修改其长度表示当前后台操作的完成情况。因为进度条会移动,所以长时间加载某些资源或者执行某些耗时的操作时,不会使用户界面失去响应。ProgressBar类的使用非常简单,只需将其显示到前台,然后启动一个后台线程定时更改表示进度的数值即可。

以下ProgressBar跟Handle结合,模拟进度条的使用,当进度条完成时会跳转到TestActivity

main.xml布局文件

<"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" mce_style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" android:max="100" /> <> <!--<ProgressBar android:id="@+id/progressBar" mce_style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" />--> <Button android:id="@+id/start" android:text="启动进度条" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/stop" android:text="停止进度条" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

PbActivity类

package com.ljq.pb; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class PbActivity extends Activity { private ProgressBar progressBar = null; private Button start = null, stop = null; // 定义Handler对象 private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar) findViewById(R.id.progressBar); progressBar.setProgress(0); start = (Button) findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { handler.post(runnable); //开始执行 } }); stop=(Button)findViewById(R.id.stop); stop.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { handler.removeCallbacks(runnable);//停止执行 progressBar.setProgress(0); } }); } int pro=0; Runnable runnable=new Runnable(){ public void run() { progressBar.setVisibility(View.VISIBLE); pro=progressBar.getProgress()+10; progressBar.setProgress(pro); //如果进度小于100,,则延迟1000毫秒后重复执行runnable if(pro<100){ handler.postDelayed(runnable, 1000); }else{ progressBar.setVisibility(View.GONE); startActivity(new Intent(PbActivity.this, TestActivity.class)); handler.removeCallbacks(runnable); progressBar.setProgress(0); } } }; }

运行结果

Android控件之ProgressBar用法实例分析1

希望本文所述对大家的Android程序设计有所帮助。

【Android控件之ProgressBar用法实例分析】相关文章:

Android之网络通信案例分析

android广播接收者实例

Android三种菜单实例分析

Android动画之3D翻转效果实现函数分析

android生命周期深入分析(二)

Android控件系列之Shape使用方法

Android CheckBox 的使用案例分析

Android 混淆代码详解及实例

Android三种GSM手机定位技术分析

Android SharedPreferences的使用分析

精品推荐
分类导航