1,ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度。
2,ProgressBar的关键属性:
*android:max="100"——最大显示进度
*android:progress="50"——第一显示进度
*android:secondaryProgress="80"——第二显示进度
*android:indeterminate="true" ——设置是否不精确显示进度。true表示不精确显示进度,false表示精确显示进度
3,ProgressBar的关键方法:
*setProgress(int)设置第一进度
*setSecondaryProgress(int)设置第二进度
*getProgress()获取第一进度
*getSecondaryProgress() 获取第二进度
*incrementProgressBy(int) 增加或减少第一进度
*incrementSecondaryProgressBy(int)增加或减少第二进度
*getMax() 获取最大进度
4,XML重要属性:
*android:progressBarStyle:默认进度条样式
*android:progressBarStyleHorizontal:水平样式
5,layout布局中的代码:
<span style="font-size:18px;"><ProgressBar //不精确显示进度的进度条(转啊转)
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar //精确显示进度的进度条
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 //用来显示进度比例
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);
//启用窗口特征,启用带进度和不带进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//显示两种进度条
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
//标题进度条的最大值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,获取进度条的第一进度
int first=progressBar.getProgress();
//2,获取进度条的第二进度
int second=progressBar.getSecondaryProgress();
//3,获取进度条的最大值
int max=progressBar.getMax();
//4,设置文本框中的进度百分比
text.setText("第一进度百分比:"+(int)(first/(float)max*100)+"%"+
" 第二进度百分比:"+(int)(second/(float)max*100)+"%");
//5,对三个按钮控件设置监听事件
add.setOnClickListener(this);
reduce.setOnClickListener(this);
reset.setOnClickListener(this);
}
//通过实现接口的方式来实现对控件的监听
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add:
{
//增加第一进度和第二进度10个刻度
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce:
{
//减少第一进度和第二进度10个刻度
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset:
{
//把第一进度和第二进度设置为原来的比例
progressBar.setProgress(50);
progressBar.setSecondaryProgress(80);
break;
}
default:
break;
}
}
//每次点击按钮,都会触发onClick事件,这样每次都可以对text进行更新处理
text.setText("第一进度百分比:"+(int)(proressBar.getProgress()/(float)proressBar.getMax()*100)+"% "+
"第二进度百分比:"+(int)(proressBar.getSecondaryProgress()/(float)proressBar.getMax()*100)+"%");
}</span>
实现效果: