【Android 开发】:UI控件之 ProgressBar 进度条的使用
2013-06-04 22:26
866 查看
在程序开发中,ProgressBar提供了可以向用户展示当前任务的进度。
1. 在学习之前,我们先了解一些相关知识,查看api文档 progressBar。一般最常用的方法有以下几点:
getMax() 得到最大刻度
getProgress() 得到刻度
setMax(int max) 设置最大刻度
setProgress(int progress) 设置刻度
setSecondaryProgress(int secondaryProgress) 设置第二刻度
setVisibility(int v) 设置是否可见
2. 下面我们就来练习一下刻度条的使用方法
1. 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中型圆形进度条" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大型圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平进度条" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:max="100"
android:progress="30"
android:secondaryProgress="60" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="增加进度" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="减小进度" />
</LinearLayout>
</LinearLayout>
2. Java代码
package com.android.progressdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressDemo extends Activity implements OnClickListener {
private ProgressBar progressBar;
private Button button1, button2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* 如何设置窗口有刻度的效果
* 注意requestWindowFeature()方法必须写在setContentView()方法之前
*/
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
setProgress(3500);
initComponenet();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
private void initComponenet() {
progressBar = (ProgressBar) findViewById(R.id.progressbar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
progressBar.setProgress((int) (progressBar.getProgress() * 1.2));
progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 1.2));
break;
case R.id.button2:
progressBar.setProgress((int) (progressBar.getProgress() * 0.8));
progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 0.8));
break;
}
}
}
2. 程序运行结果
1. 在学习之前,我们先了解一些相关知识,查看api文档 progressBar。一般最常用的方法有以下几点:
getMax() 得到最大刻度
getProgress() 得到刻度
setMax(int max) 设置最大刻度
setProgress(int progress) 设置刻度
setSecondaryProgress(int secondaryProgress) 设置第二刻度
setVisibility(int v) 设置是否可见
2. 下面我们就来练习一下刻度条的使用方法
1. 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中型圆形进度条" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大型圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平进度条" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:max="100"
android:progress="30"
android:secondaryProgress="60" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="增加进度" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="减小进度" />
</LinearLayout>
</LinearLayout>
2. Java代码
package com.android.progressdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressDemo extends Activity implements OnClickListener {
private ProgressBar progressBar;
private Button button1, button2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* 如何设置窗口有刻度的效果
* 注意requestWindowFeature()方法必须写在setContentView()方法之前
*/
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
setProgress(3500);
initComponenet();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
private void initComponenet() {
progressBar = (ProgressBar) findViewById(R.id.progressbar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
progressBar.setProgress((int) (progressBar.getProgress() * 1.2));
progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 1.2));
break;
case R.id.button2:
progressBar.setProgress((int) (progressBar.getProgress() * 0.8));
progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 0.8));
break;
}
}
}
2. 程序运行结果
相关文章推荐
- Android进度条控件progressbar使用方法详解
- 【Android 开发】:UI控件之 AlertDialog 对话框控件的的使用(一)
- 【Android 开发】:UI控件之 ViewPager 多页面滑动效果控件的的使用
- Android开发UI之常用控件的使用
- 【Android 开发】:UI控件之拖动条控件 SeekBar的使用方法
- 在Android的UI开发中,有时为了界面美观而需要使用虚线。在这里我自己实现一个画虚线的控件。
- android学习之四·使用android进度条控件progressbar
- 【Android 开发】:UI控件之 GridView 网格控件的使用
- 【Android 开发】:UI控件之 Gallery 画廊控件的使用
- 【Android 开发】:UI控件之 Menu 菜单的的使用(一)
- 【Android 开发】:UI控件之 Spinner 下拉列表控件的使用
- Android-高级UI控件_ProgressBar进度条
- Android UI控件系列:ProgressBar(进度条)
- Android UI控件之ProgressBar进度条简单实现
- 【Android 开发】:UI控件之 Dialogs 对话框控件的的使用(三)
- 【Android 开发】:UI控件之 ViewPager 多页面滑动效果控件的的使用(二)
- Android UI控件之ProgressBar进度条简单实现
- 【Android 开发】:UI控件之 RatingBar 评分控件的使用
- 【React Native开发】React Native控件之ProgressBarAndroid进度条讲解(12)
- 【Android 开发】:UI控件之单选按钮 RadioButton 的使用方法