Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidでシングルトンダイアログを使用する方法は?


例に入る前に、シングルトンデザインパターンとは何かを知っておく必要があります。シングルトンは、クラスのインスタンス化を1つのインスタンスのみに制限するデザインパターンです。注目すべき用途には、同時実行性の制御や、アプリケーションがデータストアにアクセスするための中央アクセスポイントの作成などがあります。

この例は、Androidでシングルトンダイアログを使用する方法を示しています

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードを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>

上記のコードでは、2つのボタンを使用しています。ユーザーが開始ボタンをクリックすると、進行状況ダイアログが表示され、終了ボタンを使用して進行状況ダイアログが閉じます。

ステップ3 −次のコードを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);
         }
      });
   }
}

上記のコードでは、singleTonExampleをシングルトンクラスとして使用しているため、singleTonExample.javaとして呼び出しを作成し、次のコードを追加します-

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();
      }
   }
}

上記のコードでは、ダイアログのコンテンツビューを追加したので、resフォルダーにlayut_progress_dialog.xmlとしてレイアウトを作成し、次のコンテンツを追加します–

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

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

Androidでシングルトンダイアログを使用する方法は?

次に、[ダイアログをシングルトンで表示]をクリックします。以下に示すように、シングルトンクラスのダイアログが表示されます–

Androidでシングルトンダイアログを使用する方法は?


  1. LocalBroadcastManagerの使用方法は?

    この例は、LocalBroadcastManagerの使用方法を示しています ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http:

  2. AndroidでviewFlipperを使用する方法は?

    この例は、AndroidでviewFlipperを使用する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout    xmlns:android=&q