如何在Android中使用單例模式的AlertDialog?
在進入示例之前,我們應該瞭解什麼是單例設計模式。單例是一種設計模式,它限制類的例項化只能有一個例項。值得注意的用途包括控制併發以及建立應用程式訪問其資料儲存的中心訪問點。
此示例演示如何在Android中使用單例模式的AlertDialog。
步驟1 - 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。
步驟2 - 將以下程式碼新增到res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout 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" tools:context = ".MainActivity" android:orientation = "vertical"> <Button android:id = "@+id/show" android:text = "Alert Dialog from singleTone" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </LinearLayout>
在上面的程式碼中,我們添加了一個按鈕。當用戶點選“顯示”按鈕時,它將顯示來自單例類的AlertDialog。
步驟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 show; singleTonExample singletonexample; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = findViewById(R.id.show); singletonexample = singleTonExample.getInstance(); singletonexample.init(getApplicationContext()); show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { singletonexample.AlertDialog(MainActivity.this); } }); } }
在上面的程式碼中,我們使用了**singleTonExample**作為單例類,因此建立一個名為**singleTonExample.java**的呼叫並新增以下程式碼 -
package com.example.andy.myapplication; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.speech.tts.TextToSpeech; import android.widget.Toast; public class singleTonExample { static TextToSpeech t1; private static singleTonExample ourInstance = new singleTonExample(); private Context appContext; private singleTonExample() { } public static Context get() { return getInstance().getContext(); } public static synchronized singleTonExample getInstance() { return ourInstance; } public void init(Context context) { if (appContext = = null) { this.appContext = context; } } private Context getContext() { return appContext; } public void AlertDialog(final MainActivity mainActivity) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mainActivity); alertDialogBuilder.setMessage("Are you sure, You wanted to make decision"); alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(mainActivity, "You clicked yes button", Toast.LENGTH_LONG).show(); } }); alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mainActivity.finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }
讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要在Android Studio中執行該應用程式,請開啟專案中的一個活動檔案,然後點選工具欄中的執行圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -
現在點選上面的按鈕,它將顯示來自單例類的AlertDialog,如下所示 -。
點選這裡下載專案程式碼
廣告