基本视图——ProgressBar

作者:追风剑情 发布于:2015-8-25 22:09 分类:Android

示例一

视图


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <ProgressBar android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


代码


package com.example.androidtest;

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

public class BasicViews2Activity extends Activity {

	static int progress;
	ProgressBar progressBar;
	int progressStatus = 0;
	Handler handler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basic_views2);
		
		progress = 0;
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
		
		new Thread(new Runnable(){
			public void run(){
				while (progressStatus < 10){
					progressStatus = doSomeWork();
				}
				
				handler.post(new Runnable(){
					public void run(){
						//View.INVISIBLE : 隐藏(仍旧在活动中占据空间)
						//View.GONE : 从活动中移除,它不再占据任何空间。
						progressBar.setVisibility(View.GONE);
					}
				});
			}
		}).start();
	}
	
	private int doSomeWork(){
		try{
			Thread.sleep(500);
		} catch (InterruptedException e){
			e.printStackTrace();
		}
		return ++progress;
	}
}


运行效果

p1.png

示例二

视图


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <ProgressBar android:id="@+id/progressbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal" />
</LinearLayout>


代码


package com.example.androidtest;

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

public class BasicViews2Activity extends Activity {

	static int progress;
	ProgressBar progressBar;
	int progressStatus = 0;
	Handler handler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basic_views2);
		
		progress = 0;
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
		progressBar.setMax(200);
		
		new Thread(new Runnable(){
			public void run(){
				while (progressStatus < 200){
					progressStatus = doSomeWork();
					handler.post(new Runnable(){
						public void run(){
							progressBar.setProgress(progressStatus);
						}
					});
				}
				
				handler.post(new Runnable(){
					public void run(){
						//View.VISIBLE : 显示
						//View.INVISIBLE : 隐藏(仍旧在活动中占据空间)
						//View.GONE : 从活动中移除,它不再占据任何空间。
						progressBar.setVisibility(View.GONE);
					}
				});
			}
		}).start();
	}
	
	private int doSomeWork(){
		try{
			Thread.sleep(500);
		} catch (InterruptedException e){
			e.printStackTrace();
		}
		return ++progress;
	}
}


运行效果

p2.png


ProgressBar样式还可以使用以下常量:

Widget.ProgressBar.Horizontal

Widget.ProgressBar.Small

Widget.ProgressBar.Large

Widget.ProgressBar.Inverse

Widget.ProgressBar.Small.Inverse

Widget.ProgressBar.Large.Inverse

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号