News & Events
Bài 57: Hiển thị tốc độ xử lý một hoạt động trong Android
- 12/08/2015
- Posted by: Bùi Đạt
- Category: Hướng dẫn học lập trình mobile app
Cách dễ nhất để thực hiện một vòng tròn tiến độ được sử dụng thông qua một lớp được gọi là ProgressDialog. Thanh tải cũng có thể được thực hiện thông qua lớp đó. Sự khác biệt duy nhất hợp lý giữa thanh và hình tròn, mà trước đây được sử dụng khi bạn biết tổng thời gian để chờ đợi cho một công việc cụ thể trong khi sau này được sử dụng khi bạn không biết thời gian chờ đợi Để điều này, bạn cần phải khởi tạo một đối tượng của lớp này khi hoc lap trinh android. Cú pháp của nó là.
1 | ProgressDialog progress = new ProgressDialog(this); |
Bạn có thể thiết lập một số thuộc tính trong dialog. Như định dạng, văn bản,…
1 2 3 | progress.setMessage("Downloading Music :) "); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setIndeterminate(true); |
Ngoài những phương pháp này, có những phương pháp khác được cung cấp bởi lớp ProgressDialog.
STT | Title và mô tả |
1 | getMax() Phương thức trả về giá trị lớn trong tiến trình |
2 | incrementProgressBy(int diff) Phương thức này gia tăng bar tiến trình bởi sự khác biệt giá trị như một tham số. |
3 | setIndeterminate(boolean indeterminate) Phương thức này thiết lập tiến trình như quyết định hoặc không xác định. |
4 | setMax(int max) Phương thức này thiết lập một giá trị lớn của tiến trình trong dialog. |
5 | setProgress(int value) Phương thức này được sử dụng để cập nhật tiến trình trong dialog với một số giá trị đặc biệt. |
6 | show(Context context, CharSequence title, CharSequence message) Đây là phương thức tĩnh, được sử dụng để hiển thị tiến trình trong dialog. |
Ví dụ Ví dụ này cho thấy việc sử dụng quay của hộp thoại tiến triển. Nó hiển thị một hộp thoại tiến độ quay trên nhấn nút. Để thử nghiệm với ví dụ này, bạn cần phải chạy trên một thiết bị thực tế trên sau khi phát triển các ứng dụng theo các bước dưới đây.
Các bước | Mô tả |
1 | Bạn sẽ sử dụng Android Studio để tạo ra một ứng dụng Android dưới một gói com.example.sairamkrishna.myapplication;. Trong khi tạo dự án này, chắc chắn bạn Target SDK và biên dịch với các phiên bản mới nhất của Android SDK sử dụng các cấp cao hơn của các API. |
2 | Sửa file src/ MainActivity.java thêm mã tiến để hiển thị hộp thoại tiến triển kéo sợi. |
3 | Sửa file res/layout/activity_main.xml thêm mã XML tương ứng. |
4 | Chạy ứng dụng và chọn một thiết bị Android chạy và cài đặt các ứng dụng trên nó và kiểm tra kết quả. |
Sau đây là nội dung của các sửa đổi tập tin chính hoạt động src/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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button b1; private ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarbHandler = new Handler(); private long fileSize = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); progressBarStatus = 0; fileSize = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { progressBarStatus = downloadFile(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBarbHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } if (progressBarStatus >= 100) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.dismiss(); } } }).start(); } }); } public int downloadFile() { while (fileSize <= 1000000) { fileSize++; if (fileSize == 100000) { return 10; } else if (fileSize == 200000) { return 20; } else if (fileSize == 300000) { return 30; } else if (fileSize == 400000) { return 40; } else if (fileSize == 500000) { return 50; } else if (fileSize == 700000) { return 70; } else if (fileSize == 800000) { return 80; } } return 100; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
Nội dung của res/layout/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 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Music Palyer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="download" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="112dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> </RelativeLayout> |
Nội dung của res/values/string.xml :
1 2 3 4 5 | <resources> <string name="app_name">My Application</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> </resources> |
Nội dung mặc định file AndroidManifest.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 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.progressdialog" android:versionCode="1" android:versionName="1.0" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplication.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Kết quả: Chỉ cần nhấn nút để bắt đầu Dialog Progress. Sau khi nhấn, màn hình sau sẽ xuất hiện
Trung tâm đào tạo học photoshop cơ bản với đội ngũ giáo viên trẻ tài năng, đầy nhiệt huyết và học thiet ke web cơ bản nâng cao tại VietPro!
Trả lời Hủy
Website này sử dụng Akismet để hạn chế spam. Tìm hiểu bình luận của bạn được duyệt như thế nào.