UI_组件

进度条( ProgressBar ):

我们在千千静听中都会遇到进度条的效果,进度条可以给用户提供良好的体验,Android系统已经为我们提供了ProgressBar类来完成进度条的效果,我们可以很方便的运用该类。其中常见的进度条有两中分别是“水平进度条”和“环形进度条”,在布局文件中定义两种进度条的方式比较相似,区别是,定义“水平进度条”时需要加上一项属性“style="?android:attr/progressBarStyleHorizontal"”。

ProgressBar类中常用的方法如下:

ProgressBar.setMax(intmax);设置总长度为100

ProgressBar.setProgress(intprogress);设置已经开启长度为0,假设设置为50,进度条将进行到一半停止。

下面是一个应用实例:

首先在layout文件中建一个progressbarz_layout.xml文件,代码如下:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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="fill_parent"

android:layout_height="wrap_content"

android:text="进度条演示"/>

<ProgressBar

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:max="1000"

android:progress="100"

android:id="@+id/progressbar"

/>

<ProgressBar

style="@android:style/Widget.ProgressBar.Horizontal"//该行指定该ProgressBar为水平样式。

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="1000"

android:progress="100"

android:secondaryProgress="300"

android:id="@+id/progressbar2"

/>

</LinearLayout>

效果图如下:

拖动条(SeekBar):

SeekBar.getProgress():获取拖动条当前值

调用setOnSeekBarChangeListener()方法,处理拖动条值变化事件,把SeekBar.OnSeekBarChangeListener实例作为参数传入

下面的实例可以模拟实现该进度(拖动)条,还可以通过拖动游标改变进度值,每次拖动之后仍会自动更新。

新建一个seekbar_layout.xml文件:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<SeekBar

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="1000"

android:id="@+id/seekbar"

/>

</LinearLayout>

java文件中新建一个SeekBarDemo文件:

packagecn.class3g.activity;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.widget.SeekBar;

importandroid.widget.SeekBar.OnSeekBarChangeListener;

PublicclassSeekBarDemoextendsActivityimplementsOnSeekBarChangeListener{

SeekBarseekbar=null;

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

this.setContentView(R.layout.seekbar_layout);

findViews();

}

privatevoidfindViews(){

seekbar=(SeekBar)this.findViewById(R.id.seekbar);

seekbar.setOnSeekBarChangeListener(this);

}

publicvoidonProgressChanged(SeekBarseekBar,intprogress,

booleanfromUser){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidonStartTrackingTouch(SeekBarseekBar){

Log.d("TAG","start:"+String.valueOf(seekBar.getProgress()));

}

@Override

publicvoidonStopTrackingTouch(SeekBarseekBar){

Log.d("TAG","start:"+String.valueOf(seekBar.getProgress()));

}

}

显示的效果图为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值