一、常用屬性
android:progress=”0” —-設置第一層進度條的初始值
android:max=”100” —設置進度條的最大值
android:secondaryProgress=”10” –設置第二層進度條的初始值
二、常用方法
int getMax():返回這個進度條的最大值
int getProgress();返回進度條當前進度
int getSecondProgress():返回當前次要進度
void incrementProgressBy(int diff):指定增加的進度,每次推進的步伐
boolean isInderterminate():指示進度條是否在不確定模式下
void setIndeterminate(boolean indeterminate):設置不確定模式下,用於無法確定時間任務
void setVisibility(int v):設置該進度條是否可視。
三、代碼實現
activity_main.XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.g160628_06.MainActivity">
<ProgressBar
android:id="@+id/pb_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載"
android:id="@+id/button"
android:onClick="down"
/>
</LinearLayout>
MainActivity.java文件
package com.example.g160628_06;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb_bar;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb_bar = (ProgressBar) findViewById(R.id.pb_bar);
tv_text = (TextView) findViewById(R.id.tv_text);
}
public void down(View view){
new MyDown().start();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i = msg.what;
tv_text.setText(i+"");
}
};
class MyDown extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i <= 100 ; i++) {
pb_bar.setProgress(i);
handler.sendEmptyMessage(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
實現效果