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

