亲宝软件园·资讯

展开

Android进度条按钮 Android实现带有进度条的按钮效果

安卓小飞 人气:0

安卓中带有进度条效果的按钮,如下图:

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);
       }
     });
   }
 
}

加载全部内容

相关教程
猜你喜欢
用户评论