Android实战教程第二篇之简单实现两种进度条效果
程序员文章站
2024-03-01 14:56:22
本文实例实现点击按钮模拟进度条下载进度,“下载”完成进度条消失,供大家参考,具体内容如下
代码如下:
xml:
本文实例实现点击按钮模拟进度条下载进度,“下载”完成进度条消失,供大家参考,具体内容如下
代码如下:
xml:
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <progressbar android:id="@+id/firstbar" style="?android:attr/progressbarstylehorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <progressbar android:id="@+id/secondbar" style="?android:attr/progressbarstyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="begin" /> </linearlayout>
activity:
package ydl.progressbar; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.progressbar; public class progressbartest extends activity { /** called when the activity is first created. */ //声明变量 private progressbar firstbar =null; private progressbar secondbar = null; private button mybutton = null; private int i = 0 ; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //根据控件的id来取得代表控件的对象 firstbar = (progressbar)findviewbyid(r.id.firstbar); secondbar = (progressbar)findviewbyid(r.id.secondbar); mybutton = (button)findviewbyid(r.id.mybutton); mybutton.setonclicklistener(new buttonlistener()); } class buttonlistener implements onclicklistener{ @override public void onclick(view v) { if(i == 0) { //设置进度条处于可见的状态 firstbar.setvisibility(view.visible); firstbar.setmax(150);//手动设置最大值,默认是100 secondbar.setvisibility(view.visible); } else if ( i < firstbar.getmax()){ //设置主进度条的当前值 firstbar.setprogress(i); //设置第二进度条的当前值 firstbar.setsecondaryprogress(i + 10); //因为默认的进度条无法显示进行的状态 //secondbar.setprogress(i); } else{ //设置进度条处于不可见状态 firstbar.setvisibility(view.gone); secondbar.setvisibility(view.gone); } i = i + 10 ; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。