本文实例为大家分享了Android实现带有进度条按钮效果的具体代码,供大家参考,具体内容如下
安卓中带有进度条效果的按钮,如下图:
1.布局文件如下activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
< 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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 ); } }); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_33847549/article/details/77947232