在进度条的设置中,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>
有错请指