Thứ Bảy, 16 tháng 3, 2019

Học lập trình Android - Thanh tiến trình Android sử dụng ProgressDialog

Thanh tiến trình được sử dụng để hiển thị tiến trình của một nhiệm vụ. Ví dụ: khi bạn đang tải lên hoặc tải xuống một cái gì đó từ internet, tốt hơn là hiển thị tiến trình tải xuống / tải lên cho người dùng.

Học lập trình Android chuyên sâu
Học lập trình Android chuyên sâu 

>> Trong Android có một lớp gọi là ProgressDialog cho phép bạn tạo thanh tiến trình. Để làm điều này, bạn cần khởi tạo một đối tượng của lớp này. Cú pháp của nó là Khóa học lập trình Android <<

ProgressDialog progress = new ProgressDialog(this);

Bây giờ bạn có thể thiết lập một số thuộc tính của hộp thoại này. Chẳng hạn như, phong cách của nó, văn bản của nó, vv
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
Ngoài các phương thức này, còn có các phương thức khác được cung cấp bởi lớp ProgressDialog

KhôngMô tả tiêu đề
1getMax ()
Phương pháp này trả về giá trị tối đa của tiến trình.
2gia tăng ProTHERBy (int diff)
Phương pháp này làm tăng thanh tiến trình bằng chênh lệch giá trị được truyền dưới dạng tham số.
3setIneterminate (boolean không xác định)

Phương pháp này đặt chỉ báo tiến trình là xác định hoặc không xác định.
4setMax (int max)
Phương pháp này đặt giá trị tối đa của hộp thoại tiến trình.
5setProTHER (int value)

Phương pháp này được sử dụng để cập nhật hộp thoại tiến trình với một số giá trị cụ thể.
6hiển thị (Bối cảnh bối cảnh, tiêu đề CharSequence, thông báo CharSequence)

Đây là một phương thức tĩnh, được sử dụng để hiển thị hộp thoại tiến trình.
Ví dụ này cho thấy việc sử dụng ngang của hộp thoại tiến trình trong thực tế là một thanh tiến trình. Nó hiển thị một thanh tiến trình khi nhấn nút.

Để thử nghiệm với ví dụ này, bạn cần chạy nó trên một thiết bị thực tế sau khi phát triển ứng dụng theo các bước dưới đây.

Các bướcSự miêu tả
1Bạn sẽ sử dụng studio Android để tạo một ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã tiến trình để hiển thị hộp thoại tiến trình.
3Sửa đổi tệp res / layout / Activity_main.xml để thêm mã XML tương ứng.
4Chạy ứng dụng và chọn một thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả.
Sau đây là nội dung của tệp hoạt động chính được sửa đổi src / MainActivity.java.
package com.example.sairamkrishna.myapplication;

import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
   Button b1;
   private ProgressDialog progress;
   
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1 = (Button) findViewById(R.id.button2);
   }
   
   public void download(View view){
      progress=new ProgressDialog(this);
      progress.setMessage("Downloading Music");
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.setProgress(0);
      progress.show();
      
      final int totalProgressTime = 100;
      final Thread t = new Thread() {
         @Override
         public void run() {
            int jumpTime = 0;
            
            while(jumpTime < totalProgressTime) {
               try {
                  sleep(200);
                  jumpTime += 5;
                  progress.setProgress(jumpTime);
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      };
      t.start();
   }
}
Sửa đổi nội dung của res / layout / Activity_main.xml thành như sau
<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:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Progress bar" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Download"
      android:onClick="download"
      android:id="@+id/button2"
      android:layout_marginLeft="125dp"
      android:layout_marginStart="125dp"
      android:layout_centerVertical="true" />
      
</RelativeLayout>
Đây là AndroidManifest.xml mặc định
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
   
      <activity
         android:name=".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>

Hãy thử chạy ứng dụng của bạn. Chúng tôi giả sử, bạn đã kết nối thiết bị Android Mobile thực tế của mình với máy tính.

Để chạy ứng dụng từ studio Android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.

Trước khi bắt đầu ứng dụng của bạn, studio Android sẽ hiển thị cửa sổ sau để chọn tùy chọn nơi bạn muốn chạy ứng dụng Android của mình.

Chọn thiết bị di động của bạn làm tùy chọn và sau đó kiểm tra thiết bị di động của bạn sẽ hiển thị màn hình sau

Chỉ cần nhấn nút để bắt đầu thanh Tiến trình. Sau khi nhấn, màn hình sau sẽ xuất hiện

Nó sẽ liên tục tự cập nhật.

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...