Thứ Sáu, 19 tháng 10, 2018

Android - Đang tải Spinner

Bạn có thể hiển thị tiến trình của một tác vụ trong Android thông qua thanh tiến trình tải. Thanh tiến trình có hai hình dạng. Đang tải thanh và tải Spinner. Trong chương này, chúng ta sẽ thảo luận về spinner.

Spinner được sử dụng để hiển thị tiến độ của các tác vụ đó mà tổng thời gian hoàn thành không xác định. Để sử dụng, bạn chỉ cần xác định nó trong xml như thế này.
<ProgressBar
   android:id="@+id/progressBar1"
   style="?android:attr/progressBarStyleLarge"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true" />
Sau khi xác định nó trong xml, bạn phải lấy tham chiếu của nó trong tệp java thông qua lớp ProgressBar. Cú pháp của nó được đưa ra dưới đây
private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);
Sau đó bạn có thể biến mất nó và mang nó trở lại khi cần thiết thông qua phương thức setVisibility. Cú pháp của nó được đưa ra dưới đây
spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE); 
Ngoài các phương thức này, còn có các phương thức khác được định nghĩa trong lớp ProgressBar, mà bạn có thể sử dụng để xử lý spinner hiệu quả hơn.

Sr.NoPhương pháp & mô tả
1isIndeterminate ()
Cho biết thanh tiến trình này có ở chế độ không xác định không
2postInvalidate ()
Nguyên nhân làm mất hiệu lực xảy ra trên chu kỳ tiếp theo thông qua vòng lặp sự kiện
3setIndeterminate (boolean không xác định)
Thay đổi chế độ không xác định cho thanh tiến trình này
4invalidateDrawable (Dr có thể vẽ)

Vô hiệu hóa Drawable được chỉ định
5incrementSecondaryProgressBy (int diff)

Tăng tiến độ phụ của thanh tiến trình theo số lượng được chỉ định
6getProgressDrawable ()
Có được drawable được sử dụng để vẽ thanh tiến trình trong chế độ tiến bộ

Thí dụ

Đây là một ví dụ minh họa việc sử dụng ProgressBar để xử lý spinner. Nó tạo ra một ứng dụng cơ bản cho phép bạn bật spinner khi nhấp vào nút.

Để thử nghiệm với ví dụ này, bạn có thể chạy nó trên một thiết bị thực tế hoặc trong một trình giả lập.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết.
3Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng
4Cần tạo một tệp xml trong thư mục draw.it có chứa hình dạng và xoay thông tin về thanh tiến trình
5Chạy ứng dụng và chọn 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 đã sửa đổi src / MainActivity.java .
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;


public class MainActivity extends Activity {
   Button b1;

   private ProgressBar spinner;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        
      b1=(Button)findViewById(R.id.button);
      spinner=(ProgressBar)findViewById(R.id.progressBar);
      spinner.setVisibility(View.GONE);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            spinner.setVisibility(View.VISIBLE);
         }
      });
   }
}
Sau đây là nội dung sửa đổi của xml res / layout / activity_main.xml .
Trong đoạn mã sau abc cho biết logo của tutorialspoint.com
<?xml version="1.0" encoding="utf-8"?>
<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="Progress Dialog" 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_below="@+id/imageView"
      android:layout_centerHorizontal="true" />
      
   <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" />
      
   <ProgressBar
      style="?android:attr/progressBarStyleLarge"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/progressBar"
      android:progressDrawable="@drawable/circular_progress_bar"
      android:layout_below="@+id/button"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"
      android:layout_alignLeft="@+id/textview"
      android:layout_alignStart="@+id/textview"
      android:layout_alignParentBottom="true" />

</RelativeLayout>
Sau đây là nội dung của res / drawable / circular_progress_bar.xml .
<?xml version="1.0" encoding="utf-8"?>
<rotate
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromDegrees="90"
   android:pivotX="50%"
   android:pivotY="50%"
   android:toDegrees="360">
   
   <shape
      android:innerRadiusRatio="3"
      android:shape="ring"
      android:thicknessRatio="7.0">
      
      <gradient
         android:centerColor="#007DD6"
         android:endColor="#007DD6"
         android:startColor="#007DD6"
         android:angle="0"
         android:type="sweep"
         android:useLevel="false" />
   </shape>
   
</rotate>
Sau đây là nội dung của tệp AndroidManifest.xml .
<?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="@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>
Hãy thử chạy ứng dụng của chúng tôi, chúng tôi vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.

Để chạy ứng dụng từ Android studio, 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ụ.

Lập trình Android studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, ứng dụng sẽ hiển thị cửa sổ Trình mô phỏng sau.

lập trình Android
lập trình Android
Bây giờ bấm vào nút tải spinner để bật spinner tải. Nó được thể hiện trong hình dưới đây

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 ...