zoukankan      html  css  js  c++  java
  • Android学习笔记之ProgressBar案例分析


    (1)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进度条的使用:" />
    
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="30dp" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/progressBar1"
            android:layout_marginLeft="74dp"
            android:layout_marginTop="146dp"
            android:text="下载图片" />
    
    </RelativeLayout>

    (2)

    package com.example.progressbar;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
    
    public class MainActivity extends Activity {
    
    	private Button button1;
    	private ProgressBar progressBar1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		button1 = (Button) this.findViewById(R.id.button1);
    		progressBar1 = (ProgressBar) this.findViewById(R.id.progressBar1);
    
    		progressBar1.setMax(100);// 设置最大值为100
    
    		button1.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				new MyTask().execute();
    			}
    		});
    	}
    
    	class MyTask extends AsyncTask<Void, Integer, Void> {
    		@Override
    		protected void onProgressUpdate(Integer... values) {
    			super.onProgressUpdate(values);
    			progressBar1.setProgress(values[0]);
    		}
    
    		@Override
    		protected Void doInBackground(Void... params) {
    
    			int i = 1;
    			while (i <= 100) {
    				try {
    					Thread.sleep(500);
    				} catch (Exception e) {
    					// TODO: handle exception
    				}
    				publishProgress(i);
    				i++;
    			}
    			return null;
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    

    (3)演示效果


  • 相关阅读:
    C语言main函数参数解析代码模板
    C语言函数指针复习小程序
    反转链表
    (Mingw32环境下)C语言使用库函数分配内存,按指定字节对齐
    Scrum meeting 3
    胆大妄为【DDWW】 Scrum meeting 2
    胆大妄为【DDWW】 Scrum meeting 1
    胆大妄为【DDWW】 《实验八 团队作业4:团队项目需求建模与系统设计》
    胆大妄为【DDWW】 实验七 团队作业3:团队项目需求分析与原型设计
    胆大妄为【DDWW】 实验六 团队作业2 :西北师范大学毕业生就业信息管理系统
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6984816.html
Copyright © 2011-2022 走看看