Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में सिंगलटन अलर्ट डायलॉग का उपयोग कैसे करें?

सिंगलटन पैटर्न और अलर्ट डायलॉग

सिंगलटन डिज़ाइन पैटर्न किसी क्लास के इंस्टेंशिएशन को एक इंस्टेंस तक सीमित करता है। इस उदाहरण में हम सिंगलटन क्लास से अलर्ट डायलॉग दिखाएंगे।

उदाहरण: सिंगलटन अलर्ट डायलॉग

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 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/show"
        android:text="सिंगलटन से अलर्ट डायलॉग"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

चरण 3: MainActivity.java

package com.example.singletonalertdialog;

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.showAlertDialog(MainActivity.this);
            }
        });
    }
}

चरण 4: सिंगलटन क्लास (SingletonExample.java)

package com.example.singletonalertdialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

public class SingletonExample {

    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 showAlertDialog(final MainActivity mainActivity) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mainActivity);
        alertDialogBuilder.setMessage("क्या आप निश्चित हैं? आप निर्णय लेना चाहते हैं");
        alertDialogBuilder.setPositiveButton("हाँ",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(mainActivity, "आपने हाँ बटन क्लिक किया", Toast.LENGTH_LONG).show();
                }
            });
        alertDialogBuilder.setNegativeButton("नहीं", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mainActivity.finish();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

ऐप चलाएं

ऐप लॉन्च करें और "सिंगलटन से अलर्ट डायलॉग" बटन दबाएं। सिंगलटन क्लास से अलर्ट डायलॉग दिखाई देगा जिसमें "हाँ" और "नहीं" विकल्प होंगे।

मुख्य बिंदु

  • सिंगलटन पैटर्न अलर्ट डायलॉग लॉजिक को केंद्रीकृत करता है
  • ग्लोबल कॉन्टेक्स्ट का उपयोग करके टोस्ट दिखाया जाता है
  • AlertDialog.Builder से डायलॉग बनाया जाता है
  • पॉजिटिव बटन टोस्ट दिखाता है, नेगेटिव बटन एक्टिविटी खत्म करता है
  • नोट: TextToSpeech इंपोर्ट अप्रयुक्त है, इसे हटाया जा सकता है
  1. अलर्ट डायलॉग में कस्टम व्यू कैसे जोड़ें? अलर्ट डायलॉग में कस्टम व्यू कैसे जोड़ें?

    अलर्ट डायलॉग में कस्टम व्यू यह उदाहरण अलर्ट डायलॉग में कस्टम व्यू जोड़ने को दर्शाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: res/layout/activity_main.xml में कोड जोड़ें <?xml version=1.0 encoding=utf-8?>

  1. Android अलर्ट डायलॉग में ListView कैसे दिखाएं? Android अलर्ट डायलॉग में ListView कैसे दिखाएं?

    Android AlertDialog में कस्टम ListView प्रदर्शित करना यह उदाहरण दर्शाता है कि एक कस्टम लेआउट के साथ AlertDialog में ListView कैसे दिखाया जाए, जिसमें नीचे एक बटन भी शामिल है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मेन लेआउट (res/layout/activity_main.xml) <

  1. Android में अलर्ट डायलॉग कैसे दिखाएं? Android में अलर्ट डायलॉग कैसे दिखाएं?

    Android में अलर्ट डायलॉग दिखाने का पूरा गाइड यह ट्यूटोरियल आपको Android ऐप में अलर्ट डायलॉग बनाने और दिखाने का चरणबद्ध तरीका बताता है। अलर्ट डायलॉग उपयोगकर्ता से पुष्टि लेने या महत्वपूर्ण जानकारी दिखाने के लिए उपयोगी होते हैं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio खोलें और File → New Project चु