Android實現(xiàn)帶有進度條的按鈕效果
本文實例為大家分享了Android實現(xiàn)帶有進度條按鈕效果的具體代碼,供大家參考,具體內(nèi)容如下
安卓中帶有進度條效果的按鈕,如下圖:

1.布局文件如下activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帶有進度條的Button" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="bottom" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/aa_button_gray_normal"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progress_selector" />
<Button
android:id="@+id/downLoadBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_selector"
android:text="下載" />
</RelativeLayout>
</RelativeLayout>
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);
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Android手機衛(wèi)士之手機實現(xiàn)短信指令獲取位置
這篇文章主要介紹了淺析Android手機衛(wèi)士之手機實現(xiàn)短信指令獲取位置的相關(guān)資料,需要的朋友可以參考下2016-04-04
深入解析Android App開發(fā)中Context的用法
這篇文章主要介紹了深入解析Android App開發(fā)中Context的用法,包括Context的創(chuàng)建場景和Context對資源的訪問等內(nèi)容,需要的朋友可以參考下2016-02-02
Android React Native原生模塊與JS模塊通信的方法總結(jié)
這篇文章主要介紹了Android React Native原生模塊與JS模塊通信的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android App端與PHP Web端的簡單數(shù)據(jù)交互實現(xiàn)示例
本篇文章主要介紹了Android App端與PHP Web端的簡單數(shù)據(jù)交互實現(xiàn)示例,詳細的介紹了交互的代碼,非常具有實用價值,有興趣的可以了解一下2017-10-10
android webview 簡單瀏覽器實現(xiàn)代碼
android webview 簡單瀏覽器實現(xiàn)代碼,需要的朋友可以參考一下2013-05-05
Flutter?Widget開發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

