1,ProgressBar是進(jìn)度條組件稳衬,通常用于向用戶展示某個耗時操作完成的進(jìn)度初橘。
2薪铜,ProgressBar的關(guān)鍵屬性:
*android:max="100"——最大顯示進(jìn)度
*android:progress="50"——第一顯示進(jìn)度
*android:secondaryProgress="80"——第二顯示進(jìn)度
*android:indeterminate="true" ——設(shè)置是否不精確顯示進(jìn)度吼渡。true表示不精確顯示進(jìn)度容为,false表示精確顯示進(jìn)度
3乓序,ProgressBar的關(guān)鍵方法:
*setProgress(int)設(shè)置第一進(jìn)度
*setSecondaryProgress(int)設(shè)置第二進(jìn)度
*getProgress()獲取第一進(jìn)度
*getSecondaryProgress() 獲取第二進(jìn)度
*incrementProgressBy(int) 增加或減少第一進(jìn)度
*incrementSecondaryProgressBy(int)增加或減少第二進(jìn)度
*getMax() 獲取最大進(jìn)度
4寺酪,XML重要屬性:
*android:progressBarStyle:默認(rèn)進(jìn)度條樣式
*android:progressBarStyleHorizontal:水平樣式
5,layout布局中的代碼:
<span style="font-size:18px;"><ProgressBar //不精確顯示進(jìn)度的進(jìn)度條(轉(zhuǎn)啊轉(zhuǎn))
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar //精確顯示進(jìn)度的進(jìn)度條
android:id="@+id/horizon"
android:max="100"
android:progress="50"
android:secondaryProgress="80"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ruduse" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset" />
<TextView //用來顯示進(jìn)度比例
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView></span><span style="font-size: 14px;">
</span>
6替劈,java類中的代碼:
<span style="font-size:18px;">public class MainActivity extends Activity implements OnClickListener {
private ProgressBar progressBar;
private TextView text;
private Button add,reduce,reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//啟用窗口特征寄雀,啟用帶進(jìn)度和不帶進(jìn)度的進(jìn)度條
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//顯示兩種進(jìn)度條
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
//標(biāo)題進(jìn)度條的最大值max=10000
setProgress(600);
//初始化控件
init();
}
private void init() {
progressBar=(ProgressBar) findViewById(R.id.horizon);
add=(Button) findViewById(R.id.add);
reduce=(Button) findViewById(R.id.reduce);
reset=(Button) findViewById(R.id.reset);
text=(TextView) findViewById(R.id.text);
//1,獲取進(jìn)度條的第一進(jìn)度
int first=progressBar.getProgress();
//2陨献,獲取進(jìn)度條的第二進(jìn)度
int second=progressBar.getSecondaryProgress();
//3盒犹,獲取進(jìn)度條的最大值
int max=progressBar.getMax();
//4,設(shè)置文本框中的進(jìn)度百分比
text.setText("第一進(jìn)度百分比:"+(int)(first/(float)max*100)+"%"+
" 第二進(jìn)度百分比:"+(int)(second/(float)max*100)+"%");
//5,對三個按鈕控件設(shè)置監(jiān)聽事件
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
}
//通過實現(xiàn)接口的方式來實現(xiàn)對控件的監(jiān)聽
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add:
{
//增加第一進(jìn)度和第二進(jìn)度10個刻度
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce:
{
//減少第一進(jìn)度和第二進(jìn)度10個刻度
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset:
{
//把第一進(jìn)度和第二進(jìn)度設(shè)置為原來的比例
progressBar.setProgress(50);
progressBar.setSecondaryProgress(80);
break;
}
default:
break;
}
}
//每次點擊按鈕急膀,都會觸發(fā)onClick事件沮协,這樣每次都可以對text進(jìn)行更新處理
text.setText("第一進(jìn)度百分比:"+(int)(proressBar.getProgress()/(float)proressBar.getMax()*100)+"% "+
"第二進(jìn)度百分比:"+(int)(proressBar.getSecondaryProgress()/(float)proressBar.getMax()*100)+"%");
}</span>
實現(xiàn)效果
image