本文实例为大家分享了android实现带有进度条按钮效果的具体代码,供大家参考,具体内容如下
安卓中带有进度条效果的按钮,如下图:
1.布局文件如下activity_main.xml
2.java主界面代码如下:mainactivity.java
package com.example.buttondemo; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.view; import android.widget.button; import android.widget.progressbar; import android.widget.textview; public class mainactivity extends activity { int i = 0; progressbar progressbar = null; button downloadbtn = null; handler handler = new handler() { public void handlemessage(android.os.message msg) { switch (msg.what) { case 1: i = 5; progressbar.setprogress(i); if (i != 100) { handler.sendemptymessagedelayed(new message().what = 1, 500); downloadbtn.settext(i "%"); } else if (i == 100) { downloadbtn.settext("下载完成"); // 进度条运行完成时按钮可用 downloadbtn.setenabled(true); } break; default: break; } }; }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview tx = (textview) findviewbyid(r.id.text); progressbar = (progressbar) findviewbyid(r.id.progressbar); downloadbtn = (button) findviewbyid(r.id.downloadbtn); downloadbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { i = 0; handler.sendemptymessage(new message().what = 1); // 进度条运行时按钮不可用 downloadbtn.setenabled(false); } }); } }