进度条(ProgressBar)

java.lang.Object;
android.view.View;
android.widget.ProgressBar;

ProgressBar 类方法



ProgressBar 演示样例

完整project:http://download.csdn.net/detail/sweetloveft/9416791

以下我们要学习该类中最经常使用的方法。主要是 setMax 和 setProgress 等方法。

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.progressbar.R;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        setProgress(progressHorizontal.getProgress() * 100);
        setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
        
        Button button = (Button) findViewById(R.id.increase);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());
            }
        });

        button = (Button) findViewById(R.id.decrease);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());
            }
        });

        button = (Button) findViewById(R.id.increase_secondary);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });

        button = (Button) findViewById(R.id.decrease_secondary);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(-1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });
	}
}

2.activity_main.xml

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="30dp" > <ProgressBar android:id="@+id/progress_horizontal" style="?

android:attr/progressBarStyleHorizontal" android:layout_width="200dip" android:layout_height="wrap_content" android:layout_gravity="center" android:max="100" android:progress="50" android:secondaryProgress="75" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@string/normal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="20dp" > <Button android:id="@+id/decrease" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/decrease" /> <Button android:id="@+id/increase" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/increase" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@string/custom" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="20dp" > <Button android:id="@+id/decrease_secondary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/decrease" /> <Button android:id="@+id/increase_secondary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/increase" /> </LinearLayout> </LinearLayout>

3.strings.xml

<resources>

    <string name="app_name">ProgressBar</string>
    <string name="normal">默认进度条</string>
    <string name="decrease">降低</string>
    <string name="increase">添加</string>
    <string name="custom">自己定义进度条</string>
    

</resources>

4.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sweetlover.progressbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.sweetlover.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

posted on 2017-08-11 11:21  yutingliuyl  阅读(194)  评论(0编辑  收藏  举报