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

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="帶有進(jìn)度條的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("下載完成");
// 進(jìn)度條運(yùn)行完成時按鈕可用
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);
// 進(jìn)度條運(yùn)行時按鈕不可用
downLoadBtn.setEnabled(false);
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android簽名文件轉(zhuǎn)化為pk8和pem的實現(xiàn)
這篇文章主要介紹了Android簽名文件轉(zhuǎn)化為pk8和pem的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android?DataBinding類關(guān)系深入探究
看了谷歌官方文章確實寫的太簡略了,甚至看完之后有很多地方還不知道怎么回事兒或者怎么用,那么接下來我將通過文章全面介紹一下DataBinding類關(guān)系2022-11-11
Android自定義TextView實現(xiàn)文字傾斜效果
有時候Android自帶的控件無法滿足我們的某些要求,這時就需要我們自定義控件來實現(xiàn)這些功能。比如在實際開發(fā)應(yīng)用中,我們有時需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用Android TextView如何實現(xiàn)文字傾斜效果。2016-11-11
Android隱私協(xié)議提示彈窗的實現(xiàn)流程詳解
這篇文章主要介紹了Android隱私協(xié)議提示彈窗的實現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
解決Android Studio 出現(xiàn)“Cannot resolve symbo
今天在調(diào)試的時候,Android Studio報了一個莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因為這東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下2023-03-03
Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
本文主要介紹了Android 如何攔截用戶頻繁操作,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Android實現(xiàn)仿網(wǎng)易新聞的頂部導(dǎo)航指示器
這篇文章主要介紹了Android實現(xiàn)仿網(wǎng)易新聞的頂部導(dǎo)航指示器的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Android中Fragment子類及其PreferenceFragment的創(chuàng)建過程演示
這篇文章主要介紹了Android中Fragment子類及其PreferenceFragment的創(chuàng)建過程演示,PreferenceFragment用來保存Fragment的選項設(shè)置,需要的朋友可以參考下2016-05-05

