zoukankan      html  css  js  c++  java
  • Android 中带有进度条效果的按钮(Button)

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

    1.布局文件如下activity_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <TextView
    12         android:id="@+id/text"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="带有进度条的Button" />
    16 
    17     <RelativeLayout
    18         android:layout_width="fill_parent"
    19         android:layout_height="50dp"
    20         android:layout_centerHorizontal="true"
    21         android:layout_centerVertical="true"
    22         android:gravity="bottom" >
    23 
    24         <ProgressBar
    25             android:id="@+id/progressBar"
    26             style="?android:attr/progressBarStyleHorizontal"
    27             android:layout_width="fill_parent"
    28             android:layout_height="fill_parent"
    29             android:background="@drawable/aa_button_gray_normal"
    30             android:max="100"
    31             android:progress="0"
    32             android:progressDrawable="@drawable/progress_selector" />
    33 
    34         <Button
    35             android:id="@+id/downLoadBtn"
    36             android:layout_width="fill_parent"
    37             android:layout_height="fill_parent"
    38             android:layout_centerHorizontal="true"
    39             android:layout_centerVertical="true"
    40             android:background="@drawable/btn_selector"
    41             android:text="下载" />
    42     </RelativeLayout>
    43 
    44 </RelativeLayout>

     

    2.java主界面代码如下:MainActivity.java

     1 package com.example.buttondemo;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.os.Handler;
     6 import android.os.Message;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.ProgressBar;
    10 import android.widget.TextView;
    11 
    12 public class MainActivity extends Activity {
    13     int i = 0;
    14     ProgressBar progressBar = null;
    15     Button downLoadBtn = null;
    16     Handler handler = new Handler() {
    17         public void handleMessage(android.os.Message msg) {
    18             switch (msg.what) {
    19             case 1:
    20                 i += 5;
    21                 progressBar.setProgress(i);
    22                 if (i != 100) {
    23                     handler.sendEmptyMessageDelayed(new Message().what = 1, 500);
    24                     downLoadBtn.setText(i + "%");
    25                 } else if (i == 100) {
    26                     downLoadBtn.setText("下载完成");
    27                     // 进度条运行完成时按钮可用
    28                     downLoadBtn.setEnabled(true);
    29                 }
    30                 break;
    31 
    32             default:
    33                 break;
    34             }
    35         };
    36     };
    37 
    38     @Override
    39     protected void onCreate(Bundle savedInstanceState) {
    40         super.onCreate(savedInstanceState);
    41         setContentView(R.layout.activity_main);
    42         TextView tx = (TextView) findViewById(R.id.text);
    43         progressBar = (ProgressBar) findViewById(R.id.progressBar);
    44         downLoadBtn = (Button) findViewById(R.id.downLoadBtn);
    45         downLoadBtn.setOnClickListener(new View.OnClickListener() {
    46 
    47             @Override
    48             public void onClick(View v) {
    49                 i = 0;
    50                 handler.sendEmptyMessage(new Message().what = 1);
    51                 // 进度条运行时按钮不可用
    52                 downLoadBtn.setEnabled(false);
    53             }
    54         });
    55     }
    56 
    57 }
  • 相关阅读:
    leetcode 75 颜色分类 A
    leetcode525连续数组 A
    WCF无身份验证配置
    三读设计模式
    EntityFrameWork+Oracle学习笔记搭配环境(一)
    EntityFrameWork+Oracle学习笔记DBfirst(二)
    用Python解答百度测试开发算法面试题
    Python实现采集wordpress整站数据的爬虫
    吾八哥学Python(六):运算符与表达式
    吾八哥学Python(四):了解Python基础语法(下)
  • 原文地址:https://www.cnblogs.com/_ymw/p/4170714.html
Copyright © 2011-2022 走看看