android进度条ProgressBar例子
来源:互联网 发布:java se和jdk 编辑:程序博客网 时间:2024/04/28 15:05
android进度条ProgressBar例子
进行异步加载的时候,需要使用ProgressBar提示用户耐心等待。下面是ProgressBar使用的一个小例子。
布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/progressBar1" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:layout_gravity="top"/> <ProgressBar android:id="@+id/progressbar2" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ProgressBar style="@android:style/Widget.ProgressBar.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar> <ProgressBar style="@android:style/Widget.ProgressBar.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="任务完成进度" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏" /></LinearLayout>
MainActivity.java
package com.example.wuxueyou.myfirstandroidapplication;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private ProgressBar bar1; private ProgressBar bar2; private Button button1; private Button button2; private int[] imgs = new int[]{ R.drawable.p1, R.drawable.p2, R.drawable.p3, }; int flag = 0; int status = 0; Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 0x123){ bar1.setProgress(status); bar2.setProgress(status); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); bar1 = (ProgressBar) this.findViewById(R.id.progressBar1); bar2 = (ProgressBar) this.findViewById(R.id.progressbar2); button1 = (Button)this.findViewById(R.id.button1); button2 = (Button)this.findViewById(R.id.button2); new Thread(){ public void run(){ while(status < 100){ status = doWork(); handler.sendEmptyMessage(0x123); } } }.start(); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bar1.setVisibility(View.VISIBLE); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bar1.setVisibility(View.GONE); } }); } public int doWork(){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return ++status; }}
运行效果:
0 0
- android进度条ProgressBar例子
- Android线程Handler用法(二)---结合进度条例子(ProgressBar)
- Android ProgressBar 进度条
- Android ProgressBar 进度条
- android 进度条(ProgressBar)
- android 进度条(ProgressBar) [
- android------进度条ProgressBar
- android 控件 进度条(ProgressBar)
- Android中ProgressBar进度条
- android学习----ProgressBar 进度条
- android ProgressBar 进度条
- Android:ProgressBar 进度条
- android 之进度条--------progressbar
- android 进度条ProgressBar
- Android progressBar进度条
- android:progressbar实现进度条
- Android--进度条--ProgressBar--SeekBar
- Android之进度条ProgressBar
- mongoDB用户和密码的相关操作
- 名利虚荣能激发和驱动一个民族的创新吗?
- 通过blast结果选择完全overlap的序列
- system,execute,popen
- 一个数组实现两个栈
- android进度条ProgressBar例子
- android查看联系人姓名和手机号码
- ramdisk.img及boot.img的生成过程
- GW的选择
- get sequence by its start and stop location
- Linux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.
- 中文字符占用字节数在不同编码方式下的区别
- JQuery选择器
- 第3周项目1-顺序表的基本运算