Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để sử dụng hộp thoại singleton trong Android?

Trước khi đi vào một ví dụ, chúng ta nên biết mẫu thiết kế singleton là gì. Singleton là một mẫu thiết kế hạn chế việc khởi tạo một lớp cho một thể hiện duy nhất. Các công dụng đáng chú ý bao gồm kiểm soát đồng thời và tạo điểm truy cập trung tâm để ứng dụng truy cập vào kho dữ liệu của nó.

Ví dụ này trình bày Cách sử dụng hộp thoại singleton trong android

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/start"
      android:text = "start dialog in singleTone"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
   <Button
      android:id = "@+id/end"
      android:text = "end dialog singleTone"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng hai nút. Khi người dùng nhấp vào nút bắt đầu, nó sẽ hiển thị hộp thoại tiến trình và nút kết thúc được sử dụng để đóng hộp thoại tiến trình.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
   Button start;
   singleTonExample singletonexample;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      start = findViewById(R.id.start);
      singletonexample = singleTonExample.getInstance();
      findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            singletonexample.dismiss();
         }
      });
      start.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            singletonexample.show(MainActivity.this);
         }
      });
   }
}

Trong đoạn mã trên, chúng tôi đã sử dụng singleTonExample làm lớp singleton, vì vậy hãy tạo một cuộc gọi dưới dạng singleTonExample.java và thêm đoạn mã sau -

package com.example.andy.myapplication;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
public class singleTonExample {
   private Dialog dialog;
   private static final singleTonExample ourInstance = new singleTonExample();
   public static singleTonExample getInstance() {
      return ourInstance;
   }
   private singleTonExample() { }
   public void show(Context context) {
      if (dialog != null && dialog.isShowing()) {
         return;
      }
      dialog = new Dialog(context);
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.setContentView(R.layout.layout_progress_dialog);
      dialog.setCancelable(true);
      dialog.show();
   }
   public void dismiss() {
      if (dialog != null && dialog.isShowing()) {
         dialog.dismiss();
      }
   }
}

Trong đoạn mã trên, chúng tôi đã thêm chế độ xem nội dung cho hộp thoại, vì vậy hãy tạo bố cục dưới dạng layut_progress_dialog.xml trong thư mục res và thêm nội dung sau -

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:gravity = "center">
   <ProgressBar
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:id = "@+id/progress" />
</LinearLayout>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mì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 Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để sử dụng hộp thoại singleton trong Android?

Bây giờ hãy nhấp vào hiển thị hộp thoại trong singleton Nó sẽ hiển thị hộp thoại từ lớp singleton như hình dưới đây -

Làm thế nào để sử dụng hộp thoại singleton trong Android?