build.gradle (Module: app)
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 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.github.dmytrodanylyk.android-process-button:library:1.0.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
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 45 46 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <com.dd.processbutton.iml.ActionProcessButton android:id="@+id/actionButton1" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="สร้างเลขสุ่ม" android:textColor="#00f" android:textSize="20sp" app:pb_colorComplete="#ddd" app:pb_colorError="#ddd" app:pb_colorNormal="#ddd" app:pb_colorPressed="#ddd" app:pb_cornerRadius="5dp" app:pb_textComplete="เสร็จสมบูรณ์" app:pb_textError="เกิดข้อผิดพลาด!" app:pb_textProgress="กำลังดำเนินการ..." /> <com.dd.processbutton.iml.SubmitProcessButton android:id="@+id/actionButton2" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="20dp" android:text="ดาวน์โหลด" android:textColor="#00f" android:textSize="20sp" app:pb_colorComplete="#ddd" app:pb_colorNormal="#ddd" app:pb_colorPressed="#ddd" app:pb_colorProgress="#f00" app:pb_cornerRadius="5dp" app:pb_textComplete="เสร็จสมบูรณ์" app:pb_textProgress="กำลังดำเนินการ..." /> </LinearLayout> |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
package com.phaisarn.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import com.dd.processbutton.FlatButton; import com.dd.processbutton.iml.ActionProcessButton; import com.dd.processbutton.iml.SubmitProcessButton; public class MainActivity extends AppCompatActivity { private ActionProcessButton mButton1; private SubmitProcessButton mButton2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ป่มแบบ ActionProcess ต้องเซตโมหด เพราะใช้กับสถานะได้ทั้งสองแบบ mButton1 = findViewById(R.id.actionButton1); mButton1.setMode(ActionProcessButton.Mode.ENDLESS); mButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { randomProgress(); } }); //ปุ่มแบบ SubmitProcess รองรับแค่สถานะแบบ Determinate จึงไม่ต้องเซตโหมด mButton2 = findViewById(R.id.actionButton2); mButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { downloadProgress(); } }); } private void randomProgress() { mButton1.setProgress(1); //ให้เริ่มแสดง Progress final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { double n = Math.random(); if (n >= 0.99) { //ถ้าได้ 0.99 ขึ้นไป ให้ถือว่าเสร็จสมบูรณ์ mButton1.setProgress(100); handler.removeCallbacks(this); setNormalState(mButton1); //ทำให้ปุุ่มกลับสู่สถานะ Normal } else if (n <= 0.01) { //ถ้าได้ 0.01 ลงไป ให้ถือว่าเกิด Error mButton1.setProgress(-1); handler.removeCallbacks(this); setNormalState(mButton1); //ทำให้ปุุ่มกลับสู่สถานะ Normal } else { handler.postDelayed(this, 100); //ถ้าได้เลขอื่นๆ ให้สุ่มต่อไป } } }; handler.postDelayed(runnable, 100); //เรียกขึ้นมาทำงานครั้งแรก } private void downloadProgress() { final Handler handler = new Handler(); Runnable runnable = new Runnable() { int progress = 0; @Override public void run() { if (progress < 100) { //ถ้า progress ไม่ถึง 100 ให้เพิ่มไปเรื่อยๆ progress++; mButton2.setProgress(progress); handler.postDelayed(this, 100); } else { //ถ้าได้ถึง 100 แล้ว ให้ถือว่าเสร็จสมบูรณ์ mButton2.setProgress(100); setNormalState(mButton2); //กลับสู่สถานะ Normal handler.removeCallbacks(this); } } }; handler.postDelayed(runnable, 100); } private void setNormalState(final FlatButton button) { Handler handler = new Handler(); Runnable runnable = new Runnable() { //หลักการคือ ให้รอ 3 วินาทีแล้วเซตค่า progress เป็น 0 //เพื่อให้ปุ่มเข้าสู่สถานะ Normal @Override public void run() { //เนื่องจากเราใช้เมธอดนี้จัดการทั้งสองปุ่ม (Polymorphism) //แต่เนื่องจากปุ่มเหล่านั้นเป็นคนละคลาส ดังนั้นต้องเช็คก่อนว่า //ปุ่มที่ผ่านเข้ามาในเมธอดนั้นเป็นอินสแตนซ์ของคลาสใด if (button instanceof ActionProcessButton) { ((ActionProcessButton) button).setProgress(0); } else if (button instanceof SubmitProcessButton) { ((SubmitProcessButton) button).setProgress(0); } } }; handler.postDelayed(runnable, 3000); } } |