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">
   <EditText
      android:id = "@+id/editText"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:hint = "Enter text" />
   <Button
      android:id = "@+id/save"
      android:text = "save in singleTone"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

上記のコードでは、Extとボタンを編集しました。ユーザーがボタンをクリックすると、編集テキストからデータが取得され、シングルトンクラスに格納され、トーストにシングルトンクラスの値が表示されます。

ステップ3 −次のコードをsrc / MainActivity.java

に追加します
package com.example.andy.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   EditText editText;
   Button save;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      editText = findViewById(R.id.editText);
      save = findViewById(R.id.save);
      save.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if(editText.getText().toString().isEmpty()) {
               editText.setError("Enter text");
            }else{
               String editValue = editText.getText().toString();
               singleTonExample singletonexample = com.example.andy.myapplication.singleTonExample.getInstance();
               singletonexample.setText(editValue);
               Toast.makeText(MainActivity.this,singletonexample.getText(),Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

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

package com.example.andy.myapplication;
import java.security.Identity;
public class singleTonExample {
   String editValue;
   private static final singleTonExample ourInstance = new singleTonExample();
   public static singleTonExample getInstance() {
      return ourInstance;
   }
   private singleTonExample() { }
   public void setText(String editValue) {
      this.editValue = editValue;
   }
   public String getText() {
      return editValue;
   }
}

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

Androidでシングルトンクラスを使用する方法は?

上記の結果では、tutorialspoint.comとして記述しています。」ボタンをクリックすると、シングルトンクラスからデータが取得され、以下に示すようにトーストに表示されます–

Androidでシングルトンクラスを使用する方法は?


  1. Androidローダーの使い方は?

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

  2. LocalBroadcastManagerの使用方法は?

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