您的位置:首页 > 其它

进度条组件

2015-06-01 23:04 246 查看
本文章将 介绍Android中的进度条组件

(一)ProgressBar

当一个应用在后台执行时,前台界面不会有任何信息,这时用户根本不知道程序是否在执行,以及执行进度等,这时就需要使用进度条来提示程序执行的进度。在Android中,进度条使用ProgressBar表示,用于向用户显示某个耗时操作完成的百分比。

在屏幕中添加进度条,可以在XML布局文件中通过<ProgressBar>标记添加,基本语法格式如下:

<ProgressBar

属性列表>

</ ProgressBar>

ProgressBar需要设置样式style其属性如下:

@android:style/Widget.progressBar.Large //大跳跃,旋转画面的进度条

@android: style/Widget.progressBar.Small//小跳跃,旋转画面的进度条

@android: style/Widget.progressBar.Horizontal//粗细水平长条进度条

@android:attr/progressBarStyleHorizontal //细水平长条进度条

@android:attr/progressBarStyleLarge //大圆形进入条

@android:attr/progressBarStyleSmall//小圆形进入条

在引用的时候是这样的

<ProgressBar

android:id="@+id/progressBar1"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="255dp"

android:layout_height="wrap_content"

android:visibility="visible" />

ProgressBar中比较常用的属性是

android:max//设置进度条的最大值

android:progress//设置当前第一进入条

android:secondaryProgress//设置当前第二进入条

常用的方法是:

Int getMax(); //获取进度条的最大值

Int gerProgress();//获取当前第一进度值

Int getSecondaryProgress();//获取当前第二进度值

Void setMax(int max); //设置进度的最大值

Void setProgress(int progress); //设置进度的第一进度值

例程如下:

运行效果界面:



布局文件为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/sizeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件大小:"
android:textSize="20sp" />
<EditText
android:id="@+id/inputEdit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:inputType="numberDecimal"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="MB"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:visibility="visible" />
<TextView
android:id="@+id/persentView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="进度"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="@+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下     载"
android:textSize="20sp" />
</LinearLayout>


MainActivity中实现源码为:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
private ProgressBar pb;
private EditText inputET;
private Button downBtn;
private TextView showView;
private int progressValue = 0;
private String s;
private int size;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
inputET = (EditText) findViewById(R.id.inputEdit);
downBtn = (Button) findViewById(R.id.downBtn);
showView = (TextView) findViewById(R.id.persentView);
downBtn.setOnClickListener(new Listener());
}
class Listener implements OnClickListener {
@Override
public void onClick(View v) {
s = inputET.getText().toString();// 获取文本框输入的字符串
size = Integer.parseInt(s);// 将字符串转成整数
pb.setMax(size);// 设置进度条的最大值
Thread thread = new Thread(myRun);
// 开始异步执行
thread.start();
}
}
Runnable myRun = new Runnable() {
@Override
public void run() {
while (true) {
// 拿到主线程Handler的Message
Message msg = handler.obtainMessage();
// 将进度值作为消息的参数包装进去,进度自加1
msg.arg1 = progressValue++;
// 将消息发送给主线程的Handler
handler.sendMessage(msg);
if (progressValue > size)// 当Value的值大于size时退出循环
break;
try {
Thread.sleep(100);// 为了看到进度滚动效果,可以设置线程休眠时间长点
} catch (InterruptedException e) {// 线程休眠方法会出现异常,所以需要捕获异常
e.printStackTrace();
}
}
}
};
Handler handler = new Handler(new Callback() {
// public boolean handleMessage(Message arg0)是Callback的方法,
// Callback是Handler这个类的一个内部接口,而boolean handleMessage()是这个接口的函数。
@Override
public boolean handleMessage(Message msg) {
pb.setProgress(msg.arg1);// 接收另一线程的Message,参数arg1代表了进度
showView.setText((int) (msg.arg1 * 1.0 / size * 100) + "%");// 设置显示的进度,只显示整数
pb.setSecondaryProgress(msg.arg1 + 10);// 设置第二进度值
return false;
}
});
}


(二)SeekBar

SeekBar组件表示拖动条,它与进度条类似,所不同的是,拖动条允许用户拖动滑块来改变值,通常用于实现对某种数值的调节。例如,调节图片的透明度或是音量等。

在Android中,如果想在屏幕中添加拖动条,可以在XML布局文件中通过<SeekBar>标记添加,基本语法格式如下:

<SeekBar

android:layout_height="wrap_content"

android:id="@+id/seekBar1"

android:layout_width="match_parent">

</SeekBar>

SeekBar组件允许用户改变拖动滑块的外观,这可以使用android:thumb属性实现,该属性的属性值为一个Drawable对象,这个Drawable对象将作为自定义滑块。

例程如下:

运行效果界面:



布局文件为:

<?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:text="当前值:50"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:max="100"
android:progress="50"
android:padding="10px"
android:layout_width="match_parent"/>
</LinearLayout>


MainActivity中实现源码为:

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
private SeekBar seekbar;	//拖动条

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView result=(TextView)findViewById(R.id.textView1);
seekbar = (SeekBar) findViewById(R.id.seekBar1);	//获取拖动条
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "结束滑动", Toast.LENGTH_SHORT).show();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "开始滑动", Toast.LENGTH_SHORT).show();
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
result.setText("当前值:"+progress);				//修改文本视图的值

}
});

}
}

(三)RatingBar

星级评分条与进度条类似,都允许用户拖动来改变进度,所不同的是,星级评分条通过星星表示进度。通常情况,使用星级评分条表示对某一事物的支持度或是对某种服务的满意程度等。例如,淘宝网中对卖家的好评度,就是通过星级评分条实现的。

在Android中,如果想在屏幕中添加星级评分条,可以在XML布局文件中通过<RatingBar>标记添加,基本语法格式如下:

<RatingBar

属性列表>

</RatingBar>

例程如下:

运行效果界面:



布局文件为:

<?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"
>
<!-- 星级评分条 -->
<RatingBar
android:id="@+id/ratingBar1"
android:numStars="5"
android:rating="3.5"
android:isIndicator="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="提交"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>


MainActivity中实现源码为:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

public class MainActivity extends Activity {
private RatingBar ratingbar;	//星级评分条

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ratingbar = (RatingBar) findViewById(R.id.ratingBar1);	//获取星级评分条

Button button=(Button)findViewById(R.id.button1);		//获取提交按钮
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int result=ratingbar.getProgress();	//获取进度
float rating=ratingbar.getRating();	//获取等级
float step=ratingbar.getStepSize();	//获取每次最少要改变多少个星级
Log.i("星级评分条","step="+step+" result="+result+" rating="+rating);
Toast.makeText(MainActivity.this, "你得到了"+rating+"颗星", Toast.LENGTH_SHORT).show();

}
});

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: