1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击
2. 所需要的图片
3. 代码
MainActivity
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;
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
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 );
}
} );
}
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.setClickable ( false );
}
break;
default :
break;
}
};
};
}
Layout
<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" >
<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>
btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="@android:color/transparent"></item>
<item android:state_pressed="true" android:drawable="@drawable/aa_button_gray_pressed"></item>
</selector>
progress_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<clip
android:clipOrientation="horizontal"
android:drawable="@drawable/aa_button_normal"
android:gravity="left" />
</item>
</layer-list>
每天进步一点点