這篇文章主要講解了Android如何實現帶有進度條的按鈕效果,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
本文實例為大家分享了Android實現帶有進度條按鈕效果的具體代碼,供大家參考,具體內容如下
安卓中帶有進度條效果的按鈕,如下圖:

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"
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);
}
});
}
}看完上述內容,是不是對Android如何實現帶有進度條的按鈕效果有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。