在進度條的設置中,setIndeterminate()方法是用來設置進度條是否採用‘模糊模式’(這是我杜撰的名稱,但意思沒 錯,Indeterminate的意思就是‘不確定的,模糊的,不明確的’)
當設置setIndeterminate(true)參數爲真時,進度條採用不明確顯示進度的‘模糊模式’,
當設置setIndeterminate(false)參數爲假時, 進度條不採用‘模糊模式’,而採用明確顯示進度的‘明確模式’,
具體解析介紹,見下圖:
另附代碼:
java主程序:
package com.example.threadtest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
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 {
ProgressBar m_ProgressBar01;
ProgressBar m_ProgressBar02;
Button m_Button01;
static final int msg1= 0x1;
static final int msg2=0x2;
protected int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_ProgressBar01=(ProgressBar)findViewById(R.id.ProgressBar01);
m_ProgressBar02=(ProgressBar)findViewById(R.id.ProgressBar02);
m_Button01=(Button)findViewById(R.id.Button01);
m_ProgressBar01.setIndeterminate(true);
m_ProgressBar02.setIndeterminate(false);
//監聽器
m_Button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//點擊後設置爲可見
m_ProgressBar01.setVisibility(View.VISIBLE);
m_ProgressBar02.setVisibility(View.VISIBLE);
m_ProgressBar01.setMax(100);
m_ProgressBar02.setMax(100);
//給連個進度條設置初始進度爲0
m_ProgressBar01.setProgress(0);
m_ProgressBar02.setProgress(0);
//以線程來控制進度曾增加
new Thread(new Runnable(){
public void run(){
for(int i=0;i<=10;i++){
count=(i+1)*10;//每次加10進度
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i<10){
Message msg=new Message();
msg.what =msg1;
handler.sendMessage(msg);
}
else{
Message msg=new Message();
msg.what =msg2;
handler.sendMessage(msg);
}
}
}
}).start();
}});
}
Handler handler=new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case msg1:
m_ProgressBar01.setProgress(count);
m_ProgressBar02.setProgress(count);
break;
case msg2:
m_ProgressBar01.setProgress(count);
m_ProgressBar02.setProgress(count);
m_ProgressBar01.setVisibility(View.GONE);
m_ProgressBar02.setVisibility(View.GONE);
//Thread.interrupted();
break;
}
}
};
@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;
}
}
佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="setIndeterminate(true)當參數設置爲‘真’,如下圖:"
android:textSize="20sp"
/>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條循環滾動,只反映出進程在進行,但不確定進程到了哪個進度"
android:textSize="25sp"
android:textColor="#1040A9"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="===================="
android:textSize="25sp"
android:textColor="#FF9900"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="setIndeterminate(false)當參數設置爲‘假’,如下圖:"
android:textSize="20sp"
/>
<ProgressBar
android:id="@+id/ProgressBar02"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進度條遞增滾動,可確定地反映出當前進程到了哪個進度"
android:textSize="25sp"
android:textColor="#1040A9"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始" />
</LinearLayout>
有錯請指