Android进度条使用详解(一)
- <ProgressBar android:id="@+id/progressBar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@android:style/Widget.ProgressBar.Large"/>
- public class MainActivity extends ActionBarActivity {
- private int[] data = new int[100];
- int hasData = 0;
- int status = 0;
- ProgressBar bar, bar1;
- // 创建一个负责更新的进度的Handler
- Handler handler = new Handler() {
- public void handleMessage(android.os.Message msg) {
- if (msg.what == 0x111) {
- bar.setProgress(status);
- bar1.setProgress(status);
- }
- };
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- bar = (ProgressBar) findViewById(R.id.progressBar);
- bar1 = (ProgressBar) findViewById(R.id.progressBar1);
- // 启动线程来执行任务
- new Thread(new Runnable() {
- public void run() {
- while (status < 100) {
- status = doWork(); // 获取耗操作的完成百分比
- handler.sendEmptyMessage(0x111); // 发送消息
- }
- }
- }).start();
- }
- // 模拟一个耗时任务
- public int doWork() {
- data[hasData++] = (int) (Math.random() * 100);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return hasData;
- }
- }
- <ProgressBar
- android:id="@+id/progressBar"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="100dp"
- android:max="100" />
- <ProgressBar
- android:id="@+id/progressBar1"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="100dp"
- android:max="100"
- android:progressDrawable="@drawable/mybar" />
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <!-- 定义轨道的背景 -->
- <item
- android:id="@android:id/background"
- android:drawable="@drawable/ic_launcher"/>
- <!-- 定义轨道上已经完成部分的样式 -->
- <item
- android:id="@android:id/progress"
- android:drawable="@drawable/android"/>
- </layer-list>
- final ProgressDialog mypDialog=new ProgressDialog(MainActivity.this); //实例化一个进度条对话框
- mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //设置进度条风格
- mypDialog.setTitle("进度条对话框演示"); //设置进度条标题
- mypDialog.setMessage("任务正在执行中,请稍后"); //设置提示信息
- mypDialog.setIcon(R.drawable.android); //设置图标
- mypDialog.setIndeterminate(true); //设置是否精度显示进度
- mypDialog.setCancelable(false); //设置是否触屏取消
- mypDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消执行", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) { //设置按钮
- mypDialog.dismiss();
- } });
- mypDialog.show();
- myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);(myProgressBar.incrementProgressBy(5);)
- mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);(myProgressBar.incrementProgressBy(-5);)
- myprogress=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
- AlertDialog alertDialog = new AlertDialog(this);
- LayoutInflater layoutInflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
- LinearLayout myLayout=(LinearLayout) layoutInflater.inflate(R.layout.myview, null);
- myProgressBar.setProgress(Tag); //进度条进度初始值为0
- alertDialog.setTitle("长形进度条");
- alertDialog.setIcon(R.drawable.Android);
- alertDialog.setMessage("测试View加入进度条");
- alertDialog.setView(myLayout);
- alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
- public void onClick(DialogInterface dialog, int which) {
- Tag=myProgressBar.getProgress(); //获得当前进度条的值
- }});
- AlterD.show();
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal" >
- <Button
- android:id="@+id/myView_BT_Down"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:text="-" >
- </Button>
- <ProgressBar
- android:id="@+id/myView_ProgressBar"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="178dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:progress="57" >
- </ProgressBar>
- <Button
- android:id="@+id/myView_BT_Up"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:text="+" >
- </Button>
- </LinearLayout>
- protected void onCreate(Bundle savedInstanceState) {
- //设置窗口特征:启动显示进度的进度条
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- //设置窗口特征:启动显示不带进度的进度条
- // requestWindowFeature(Window.FEATURE_PROGRESS);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn1 = (Button)findViewById(R.id.btn1);
- btn1.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // setProgressBarVisibility(true);
- setProgressBarIndeterminateVisibility(true);
- }
- });
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。