डायलॉग

लिखने का तरीका आज़माएं
Android के लिए, Jetpack Compose हमारा सुझाया गया यूज़र इंटरफ़ेस (यूआई) टूलकिट है. Compose में कॉम्पोनेंट जोड़ने का तरीका जानें.

डायलॉग एक छोटी विंडो होती है. इससे उपयोगकर्ता को पर क्लिक करें या अतिरिक्त जानकारी डालें. डायलॉग पूरी स्क्रीन पर नहीं दिखता और आम तौर पर, इसे मॉडल इवेंट के लिए इस्तेमाल किया जाता है. इसके लिए, उपयोगकर्ताओं को पहले कोई कार्रवाई करनी पड़ती है तो वे आगे बढ़ सकें.

बेसिक डायलॉग दिखाने वाली इमेज
पहली इमेज. सामान्य डायलॉग.

Dialog क्लास, डायलॉग के लिए बेस क्लास है, लेकिन Dialog इंस्टैंशिएट नहीं करें सकता है. इसके बजाय, इनमें से किसी एक सब-क्लास का इस्तेमाल करें:

AlertDialog
ऐसा डायलॉग जो एक टाइटल दिखा सकता है. ज़्यादा से ज़्यादा तीन बटन और ऐसे आइटम की सूची जिन्हें चुना जा सकता है आइटम, या कोई कस्टम लेआउट.
DatePickerDialog अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है या TimePickerDialog
पहले से तय यूज़र इंटरफ़ेस (यूआई) वाला डायलॉग जो उपयोगकर्ता को कोई तारीख चुनने देता है या समय.

ये क्लास आपके डायलॉग की स्टाइल और स्ट्रक्चर को तय करती हैं. आपको इनकी ज़रूरत भी पड़ सकती है एक DialogFragment अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है अपने डायलॉग बॉक्स के कंटेनर के तौर पर. DialogFragment क्लास से मिलती है वे सभी कंट्रोल जिनकी ज़रूरत आपको अपना डायलॉग बनाने और उसके दिखने के तरीके को मैनेज करने के लिए होती है, कॉल करने के बजाय Dialog ऑब्जेक्ट पर कॉल करें.

DialogFragment का इस्तेमाल करके डायलॉग बॉक्स को मैनेज करने से यह समस्या ठीक हो जाती है लाइफ़साइकल इवेंट को हैंडल कर सकता है, जैसे कि जब उपयोगकर्ता 'वापस जाएं' बटन पर टैप करता है या घुमाता है स्क्रीन. DialogFragment क्लास आपको डायलॉग का यूआई, बड़े यूज़र इंटरफ़ेस (यूआई) में एम्बेड किए जा सकने वाले कॉम्पोनेंट के तौर पर दिखता है—ठीक ट्रेडिशनल Fragment—इस तरह के जैसे कि जब आपको डायलॉग यूज़र इंटरफ़ेस (यूआई) को बड़े और छोटे, दोनों जगह अलग-अलग दिखाना हो स्क्रीन.

इस दस्तावेज़ के नीचे दिए सेक्शन में बताया गया है कि AlertDialog के साथ DialogFragment ऑब्जेक्ट है. अगर आप तारीख या समय चुनने वाला टूल बनाना चाहते हैं, तो पढ़ने के लिए अपने ऐप्लिकेशन है.

डायलॉग फ़्रैगमेंट बनाएं

इस टूल की मदद से, अलग-अलग तरह के डायलॉग डिज़ाइन किए जा सकते हैं. इनमें, लेआउट और इनमें बताए गए लेआउट मटीरियल डिज़ाइन डायलॉगDialogFragment को बड़ा करके और AlertDialog में onCreateDialog() कॉलबैक का तरीका.

उदाहरण के लिए, यहां एक बेसिक AlertDialog है, जिसे DialogFragment:

Kotlin

class StartGameDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            // Use the Builder class for convenient dialog construction.
            val builder = AlertDialog.Builder(it)
            builder.setMessage("Start game")
                .setPositiveButton("Start") { dialog, id ->
                    // START THE GAME!
                }
                .setNegativeButton("Cancel") { dialog, id ->
                    // User cancelled the dialog.
                }
            // Create the AlertDialog object and return it.
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

class OldXmlActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_old_xml)

        StartGameDialogFragment().show(supportFragmentManager, "GAME_DIALOG")
    }
}

Java

public class StartGameDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction.
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_start_game)
               .setPositiveButton(R.string.start, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // START THE GAME!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancels the dialog.
                   }
               });
        // Create the AlertDialog object and return it.
        return builder.create();
    }
}
// ...

StartGameDialogFragment().show(supportFragmentManager, "GAME_DIALOG");

इस क्लास और कॉल के लिए इंस्टेंस बनाने पर show() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है उस ऑब्जेक्ट पर, डायलॉग दिखाई देता है, जैसा कि नीचे दी गई इमेज में दिखाया गया है.

एक इमेज, जिसमें दो ऐक्शन बटन के साथ बेसिक डायलॉग दिखाया गया है
दूसरी इमेज. एक डायलॉग बॉक्स, जिसमें दो मैसेज हैं ऐक्शन बटन का इस्तेमाल करें.

अगले सेक्शन में, AlertDialog.Builder अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है डायलॉग बनाने के लिए API.

आपका डायलॉग कितना मुश्किल है, इसके आधार पर आपकी रणनीति के कई अन्य DialogFragment में कॉलबैक के अन्य तरीके. इनमें सभी फ़्रैगमेंट लाइफ़साइकल के बुनियादी तरीके.

सूचना वाला डायलॉग बॉक्स बनाएं

AlertDialog क्लास की मदद से, कई तरह के डायलॉग बनाए जा सकते हैं डिज़ाइन किया जा सकता है और अक्सर इसी तरह की डायलॉग क्लास की ज़रूरत होती है. जैसा कि नीचे दिखाया गया है इमेज में, अलर्ट डायलॉग के तीन हिस्से होते हैं:

  • टाइटल: यह विकल्प ज़रूरी नहीं है. इसका इस्तेमाल सिर्फ़ तब किया जाता है, जब कॉन्टेंट एरिया जिसे किसी ज़्यादा जानकारी वाले मैसेज, सूची या कस्टम लेआउट से लिया गया हो. अगर आपको आसान मैसेज या सवाल बता सकते हैं, आपको शीर्षक की ज़रूरत नहीं है.
  • कॉन्टेंट एरिया: इससे मैसेज, सूची या दूसरी पसंद के मुताबिक बनाया जा सकता है लेआउट.
  • ऐक्शन बटन: एक क्लिक में ज़्यादा से ज़्यादा तीन ऐक्शन बटन हो सकते हैं डायलॉग.

AlertDialog.Builder क्लास एपीआई उपलब्ध कराती है. इसकी मदद से, अपनी ऐसेट बनाई जा सकती हैं इस तरह की सामग्री वाला एक AlertDialog, जिसमें कस्टम लेआउट.

AlertDialog बनाने के लिए, यह तरीका अपनाएं:

Kotlin

val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
    .setMessage("I am the message")
    .setTitle("I am the title")

val dialog: AlertDialog = builder.create()
dialog.show()

Java

// 1. Instantiate an AlertDialog.Builder with its constructor.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics.
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);

// 3. Get the AlertDialog.
AlertDialog dialog = builder.create();

पिछला कोड स्निपेट इस डायलॉग को जनरेट करता है:

इस इमेज में एक डायलॉग बॉक्स दिखाया गया है. इसमें टाइटल, कॉन्टेंट एरिया, और दो ऐक्शन बटन हैं.
तीसरी इमेज. बेसिक अलर्ट का लेआउट डायलॉग.

बटन जोड़ना

इमेज 2 में दिए गए ऐक्शन बटन के जैसे ऐक्शन बटन जोड़ने के लिए, setPositiveButton() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है और setNegativeButton() तरीका:

Kotlin

val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
    .setMessage("I am the message")
    .setTitle("I am the title")
    .setPositiveButton("Positive") { dialog, which ->
        // Do something.
    }
    .setNegativeButton("Negative") { dialog, which ->
        // Do something else.
    }

val dialog: AlertDialog = builder.create()
dialog.show()

Java

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons.
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User taps OK button.
           }
       });
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancels the dialog.
           }
       });
// Set other dialog properties.
...

// Create the AlertDialog.
AlertDialog dialog = builder.create();

set...Button() तरीकों में, बटन— string संसाधन—और एक DialogInterface.OnClickListener जो उपयोगकर्ता के बटन पर टैप करने पर की जाने वाली कार्रवाई के बारे में बताता है.

यहां तीन ऐक्शन बटन जोड़े जा सकते हैं:

  • पॉज़िटिव: इसका इस्तेमाल कार्रवाई को स्वीकार करने और जारी रखने के लिए करें ( "ठीक है" ऐक्शन).
  • नेगेटिव: कार्रवाई रद्द करने के लिए इसका इस्तेमाल करें.
  • न्यूट्रल: इसका इस्तेमाल तब करें, जब उपयोगकर्ता कार्रवाई करता है, लेकिन उसे रद्द करना ज़रूरी नहीं है. यह पॉज़िटिव और नेगेटिव बटन. उदाहरण के लिए, कार्रवाई "मुझे याद दिलाएं बाद में चलेगा."

AlertDialog में, हर बटन टाइप में से सिर्फ़ एक बटन जोड़ा जा सकता है. इसके लिए उदाहरण के लिए, एक से ज़्यादा "पॉज़िटिव" नहीं हो सकते बटन.

पिछले कोड स्निपेट से, आपको सूचना वाला एक डायलॉग दिखता है, जैसे कि:

इस इमेज में टाइटल, मैसेज, और दो ऐक्शन बटन के साथ सूचना वाला डायलॉग बॉक्स दिख रहा है.
चौथी इमेज. टाइटल के साथ सूचना वाला डायलॉग बॉक्स, और दो ऐक्शन बटन दिखेंगे.

सूची जोड़ें

AlertDialog के साथ तीन तरह की सूचियां उपलब्ध हैं एपीआई:

  • एक विकल्प वाली पारंपरिक सूची.
  • एक विकल्प वाली स्थायी सूची (रेडियो बटन).
  • कई विकल्पों वाली स्थायी सूची (चेकबॉक्स).

पांचवीं इमेज में दी गई सूची की तरह एक विकल्प वाली सूची बनाने के लिए, setItems() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है तरीका:


Kotlin

val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
    .setTitle("I am the title")
    .setPositiveButton("Positive") { dialog, which ->
        // Do something.
    }
    .setNegativeButton("Negative") { dialog, which ->
        // Do something else.
    }
    .setItems(arrayOf("Item One", "Item Two", "Item Three")) { dialog, which ->
        // Do something on item tapped.
    }

val dialog: AlertDialog = builder.create()
dialog.show()

Java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position of the selected item.
           }
    });
    return builder.create();
}

यह कोड स्निपेट इस तरह का डायलॉग जनरेट करता है:

इस इमेज में डायलॉग बॉक्स दिखाया गया है. इसमें टाइटल और सूची है.
पांचवीं इमेज. टाइटल और सूची वाला डायलॉग बॉक्स.

डायलॉग बॉक्स के कॉन्टेंट एरिया में सूची दिखती है. इसलिए, डायलॉग नहीं दिखाया जा सकता मैसेज और सूची, दोनों दिखा सकते हैं. इसके साथ वाले डायलॉग के लिए टाइटल सेट करें setTitle(). सूची के लिए आइटम तय करने के लिए, setItems() पर कॉल करें. इसके बाद, कलेक्शन. वैकल्पिक रूप से, आप इसका उपयोग कर एक सूची setAdapter(). इससे आप सूची में डाइनैमिक डेटा का इस्तेमाल कर सकते है. उदाहरण के लिए, डेटाबेस—किसी भी समस्या को हल करने के लिए, ListAdapter.

अगर सूची को फिर से बनाने के लिए ListAdapter का इस्तेमाल किया जाता है, तो हमेशा Loader ताकि कॉन्टेंट एसिंक्रोनस रूप से लोड हो सके. इसके बारे में इसमें आगे बताया गया है लेआउट बनाएं अडैप्टर के साथ और लोडर.

कई विकल्पों वाले विकल्प या एक विकल्प वाली सूची जोड़ें

कई विकल्प वाले आइटम (चेकबॉक्स) या एक विकल्प वाले आइटम की सूची जोड़ने के लिए (रेडियो बटन), तो setMultiChoiceItems() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है या setSingleChoiceItems() विधियों का भी इस्तेमाल किया जा सकता है.

उदाहरण के लिए, कई विकल्पों वाले विकल्पों में से किसी एक की सूची बनाने का तरीका जानें इमेज 6 में दिखाया गया है. इसमें चुने गए आइटम को ArrayList:

Kotlin

val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
    .setTitle("I am the title")
    .setPositiveButton("Positive") { dialog, which ->
        // Do something.
    }
    .setNegativeButton("Negative") { dialog, which ->
        // Do something else.
    }
    .setMultiChoiceItems(
        arrayOf("Item One", "Item Two", "Item Three"), null) { dialog, which, isChecked ->
        // Do something.
    }

val dialog: AlertDialog = builder.create()
dialog.show()

Java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    selectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title.
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for
    // none), and the listener through which to receive callbacks when items
    // are selected.
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checks the item, add it to the selected
                       // items.
                       selectedItems.add(which);
                   } else if (selectedItems.contains(which)) {
                       // If the item is already in the array, remove it.
                       selectedItems.remove(which);
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User taps OK, so save the selectedItems results
                   // somewhere or return them to the component that opens the
                   // dialog.
                   ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ...
               }
           });

    return builder.create();
}
इस इमेज में एक डायलॉग बॉक्स दिखाया गया है. इसमें कई विकल्प वाले आइटम की सूची है.
छठी इमेज. कई विकल्प वाले आइटम की सूची.

एक विकल्प वाला सूचना डायलॉग, इस तरह से मिल सकता है:

Kotlin

val builder: AlertDialog.Builder = AlertDialog.Builder(context)
builder
    .setTitle("I am the title")
    .setPositiveButton("Positive") { dialog, which ->
        // Do something.
    }
    .setNegativeButton("Negative") { dialog, which ->
        // Do something else.
    }
    .setSingleChoiceItems(
        arrayOf("Item One", "Item Two", "Item Three"), 0
    ) { dialog, which ->
        // Do something.
    }

val dialog: AlertDialog = builder.create()
dialog.show()

Java

        String[] choices = {"Item One", "Item Two", "Item Three"};
        
        AlertDialog.Builder builder = AlertDialog.Builder(context);
        builder
                .setTitle("I am the title")
                .setPositiveButton("Positive", (dialog, which) -> {

                })
                .setNegativeButton("Negative", (dialog, which) -> {

                })
                .setSingleChoiceItems(choices, 0, (dialog, which) -> {

                });

        AlertDialog dialog = builder.create();
        dialog.show();

इसका नतीजा यह होता है कि:

इस इमेज में एक डायलॉग बॉक्स दिखाया गया है. इसमें एक विकल्प वाले आइटम की सूची है.
सातवीं इमेज. एक विकल्प वाले आइटम की सूची.

अपनी पसंद का लेआउट बनाना

अगर आपको किसी डायलॉग बॉक्स में पसंद के मुताबिक लेआउट चाहिए, तो लेआउट बनाएं और उसे किसी डायलॉग बॉक्स में जोड़ें कॉल करके AlertDialog setView() आपके AlertDialog.Builder ऑब्जेक्ट पर.

कस्टम डायलॉग लेआउट दिखाने वाली इमेज.
आठवीं इमेज. पसंद के मुताबिक डायलॉग लेआउट.

डिफ़ॉल्ट रूप से, कस्टम लेआउट डायलॉग विंडो में भर जाता है, लेकिन आप अब भी इसका इस्तेमाल कर सकते हैं बटन और टाइटल जोड़ने के लिए AlertDialog.Builder तरीके.

उदाहरण के लिए, यहां पिछले कस्टम डायलॉग की लेआउट फ़ाइल दी गई है लेआउट:

res/layout/dialog_signin.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:src="@drawable/header_logo"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:scaleType="center"
        android:background="#FFFFBB33"
        android:contentDescription="@string/app_name" />
    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/username" />
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="@string/password"/>
</LinearLayout>

अपने DialogFragment के लेआउट को बढ़ाने के लिए, LayoutInflater के साथ getLayoutInflater() और कॉल करें inflate(). पहला पैरामीटर लेआउट रिसॉर्स आईडी है और दूसरा पैरामीटर लेआउट के लिए पैरंट व्यू. इसके बाद, आप कॉल कर सकते हैं setView() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है डायलॉग बॉक्स में लेआउट रखने के लिए. यह नीचे दिए गए उदाहरण में दिखाया गया है.

Kotlin

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return activity?.let {
        val builder = AlertDialog.Builder(it)
        // Get the layout inflater.
        val inflater = requireActivity().layoutInflater;

        // Inflate and set the layout for the dialog.
        // Pass null as the parent view because it's going in the dialog
        // layout.
        builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                // Add action buttons.
                .setPositiveButton(R.string.signin,
                        DialogInterface.OnClickListener { dialog, id ->
                            // Sign in the user.
                        })
                .setNegativeButton(R.string.cancel,
                        DialogInterface.OnClickListener { dialog, id ->
                            getDialog().cancel()
                        })
        builder.create()
    } ?: throw IllegalStateException("Activity cannot be null")
}

Java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater.
    LayoutInflater inflater = requireActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog.
    // Pass null as the parent view because it's going in the dialog layout.
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // Sign in the user.
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });
    return builder.create();
}

अगर आपको कस्टम डायलॉग चाहिए, तो इसके बजाय Activity डायलॉग का इस्तेमाल करने के बजाय, Dialog API का इस्तेमाल करें. कोई गतिविधि बनाएं और इसकी थीम को Theme.Holo.Dialog अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है में <activity> मेनिफ़ेस्ट एलिमेंट:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

गतिविधि अब फ़ुलस्क्रीन के बजाय डायलॉग विंडो में दिखती है.

इवेंट को डायलॉग के होस्ट पर वापस पास करें

जब कोई व्यक्ति किसी डायलॉग के ऐक्शन बटन पर टैप करता है या कोई आइटम चुनता है तो आपका DialogFragment ज़रूरी काम कर सकता है कार्रवाई नहीं होती है, लेकिन अक्सर आप उस गतिविधि को इवेंट को देना चाहते हैं या वह फ़्रैगमेंट जिससे डायलॉग बॉक्स खुलता है. ऐसा करने के लिए, एक का इस्तेमाल किया जा सकता है. इसके बाद, उस इंटरफ़ेस को होस्ट कॉम्पोनेंट, जिसे डायलॉग से ऐक्शन इवेंट मिलते हैं.

उदाहरण के लिए, यहां एक DialogFragment दिया गया है, जो किसी इंटरफ़ेस के बारे में बताता है इससे, इवेंट को होस्ट की गतिविधि पर वापस भेजा जाता है:

Kotlin

class NoticeDialogFragment : DialogFragment() {
    // Use this instance of the interface to deliver action events.
    internal lateinit var listener: NoticeDialogListener

    // The activity that creates an instance of this dialog fragment must
    // implement this interface to receive event callbacks. Each method passes
    // the DialogFragment in case the host needs to query it.
    interface NoticeDialogListener {
        fun onDialogPositiveClick(dialog: DialogFragment)
        fun onDialogNegativeClick(dialog: DialogFragment)
    }

    // Override the Fragment.onAttach() method to instantiate the
    // NoticeDialogListener.
    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Verify that the host activity implements the callback interface.
        try {
            // Instantiate the NoticeDialogListener so you can send events to
            // the host.
            listener = context as NoticeDialogListener
        } catch (e: ClassCastException) {
            // The activity doesn't implement the interface. Throw exception.
            throw ClassCastException((context.toString() +
                    " must implement NoticeDialogListener"))
        }
    }
}

Java

public class NoticeDialogFragment extends DialogFragment {

    // The activity that creates an instance of this dialog fragment must
    // implement this interface to receive event callbacks. Each method passes
    // the DialogFragment in case the host needs to query it.
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events.
    NoticeDialogListener listener;

    // Override the Fragment.onAttach() method to instantiate the
    // NoticeDialogListener.
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface.
        try {
            // Instantiate the NoticeDialogListener so you can send events to
            // the host.
            listener = (NoticeDialogListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface. Throw exception.
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
    ...
}

डायलॉग को होस्ट करने वाली गतिविधि, डायलॉग फ़्रैगमेंट के कंस्ट्रक्टर को NoticeDialogListener इंटरफ़ेस को लागू करने की प्रोसेस:

Kotlin

class MainActivity : FragmentActivity(),
        NoticeDialogFragment.NoticeDialogListener {

    fun showNoticeDialog() {
        // Create an instance of the dialog fragment and show it.
        val dialog = NoticeDialogFragment()
        dialog.show(supportFragmentManager, "NoticeDialogFragment")
    }

    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following
    // methods defined by the NoticeDialogFragment.NoticeDialogListener
    // interface.
    override fun onDialogPositiveClick(dialog: DialogFragment) {
        // User taps the dialog's positive button.
    }

    override fun onDialogNegativeClick(dialog: DialogFragment) {
        // User taps the dialog's negative button.
    }
}

Java

public class MainActivity extends FragmentActivity
                          implements NoticeDialogFragment.NoticeDialogListener{
    ...
    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it.
        DialogFragment dialog = new NoticeDialogFragment();
        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
    }

    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following
    // methods defined by the NoticeDialogFragment.NoticeDialogListener
    // interface.
    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // User taps the dialog's positive button.
        ...
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User taps the dialog's negative button.
        ...
    }
}

क्योंकि होस्ट की गतिविधि NoticeDialogListener—इसे लागू करने के लिए onAttach() पिछले उदाहरण में दिखाया गया कॉलबैक तरीका—डायलॉग फ़्रैगमेंट गतिविधि को क्लिक इवेंट डिलीवर करने के लिए, इंटरफ़ेस कॉलबैक मैथड का इस्तेमाल करें:

Kotlin

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        return activity?.let {
            // Build the dialog and set up the button click handlers.
            val builder = AlertDialog.Builder(it)

            builder.setMessage(R.string.dialog_start_game)
                    .setPositiveButton(R.string.start,
                            DialogInterface.OnClickListener { dialog, id ->
                                // Send the positive button event back to the
                                // host activity.
                                listener.onDialogPositiveClick(this)
                            })
                    .setNegativeButton(R.string.cancel,
                            DialogInterface.OnClickListener { dialog, id ->
                                // Send the negative button event back to the
                                // host activity.
                                listener.onDialogNegativeClick(this)
                            })

            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }

Java

public class NoticeDialogFragment extends DialogFragment {
    ...
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers.
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_start_game)
               .setPositiveButton(R.string.start, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the positive button event back to the host activity.
                       listener.onDialogPositiveClick(NoticeDialogFragment.this);
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the negative button event back to the host activity.
                       listener.onDialogNegativeClick(NoticeDialogFragment.this);
                   }
               });
        return builder.create();
    }
}

कोई डायलॉग दिखाएं

जब आपको अपना डायलॉग दिखाना हो, तो इसका एक इंस्टेंस बनाएं DialogFragment और कॉल करें show(), पास कर दिया FragmentManager और डायलॉग फ़्रैगमेंट के लिए एक टैग नाम.

इस नंबर पर कॉल करके, आप FragmentManager पा सकते हैं getSupportFragmentManager() से FragmentActivity या कॉल करके getParentFragmentManager() Fragment से. उदाहरण के लिए यह देखें:

Kotlin

fun confirmStartGame() {
    val newFragment = StartGameDialogFragment()
    newFragment.show(supportFragmentManager, "game")
}

Java

public void confirmStartGame() {
    DialogFragment newFragment = new StartGameDialogFragment();
    newFragment.show(getSupportFragmentManager(), "game");
}

दूसरा तर्क, "game", एक ऐसा यूनीक टैग नाम है जो सिस्टम ज़रूरत पड़ने पर फ़्रैगमेंट की स्थिति को सेव और पहले जैसा करने के लिए इसका इस्तेमाल करता है. टैग भी कॉल करके फ़्रैगमेंट मैनेज करने की सुविधा मिलती है findFragmentByTag().

डायलॉग बॉक्स को फ़ुलस्क्रीन या एम्बेड किए गए फ़्रैगमेंट के तौर पर दिखाएं

आप शायद अपने यूज़र इंटरफ़ेस (यूआई) का कोई हिस्सा, डायलॉग बॉक्स के तौर पर दिखाना चाहें को फ़ुलस्क्रीन या एम्बेड किए गए फ़्रैगमेंट के तौर पर दिखाया जा सकता है. आप शायद यह भी अलग-अलग तरह से दिखाना चाहते हैं. यह सुविधा डिवाइस की स्क्रीन के साइज़ पर निर्भर करती है. कॉन्टेंट बनाने DialogFragment क्लास में ऐसा करने की सुविधा मिलती है, क्योंकि यह एम्बेड किए जा सकने वाले Fragment की तरह काम कर सकता है.

हालांकि, आप AlertDialog.Builder या अन्य इस मामले में डायलॉग बनाने के लिए Dialog ऑब्जेक्ट हैं. अगर आपको DialogFragment को एम्बेड करने के लिए, लेआउट चुनें, फिर लेआउट को onCreateView() कॉलबैक.

यहां DialogFragment का उदाहरण दिया गया है, जो कि डायलॉग के तौर पर दिख सकता है या purchase_items.xml:

Kotlin

class CustomDialogFragment : DialogFragment() {

    // The system calls this to get the DialogFragment's layout, regardless of
    // whether it's being displayed as a dialog or an embedded fragment.
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        // Inflate the layout to use as a dialog or embedded fragment.
        return inflater.inflate(R.layout.purchase_items, container, false)
    }

    // The system calls this only when creating the layout in a dialog.
    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        // The only reason you might override this method when using
        // onCreateView() is to modify the dialog characteristics. For example,
        // the dialog includes a title by default, but your custom layout might
        // not need it. Here, you can remove the dialog title, but you must
        // call the superclass to get the Dialog.
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return dialog
    }
}

Java

public class CustomDialogFragment extends DialogFragment {
    // The system calls this to get the DialogFragment's layout, regardless of
    // whether it's being displayed as a dialog or an embedded fragment.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout to use as a dialog or embedded fragment.
        return inflater.inflate(R.layout.purchase_items, container, false);
    }

    // The system calls this only when creating the layout in a dialog.
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using
        // onCreateView() is to modify the dialog characteristics. For example,
        // the dialog includes a title by default, but your custom layout might
        // not need it. Here, you can remove the dialog title, but you must
        // call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}

नीचे दिया गया उदाहरण तय करता है कि फ़्रैगमेंट को डायलॉग के तौर पर दिखाना है या एक फ़ुलस्क्रीन यूज़र इंटरफ़ेस (यूआई) जो स्क्रीन के साइज़ पर आधारित होता है:

Kotlin

fun showDialog() {
    val fragmentManager = supportFragmentManager
    val newFragment = CustomDialogFragment()
    if (isLargeLayout) {
        // The device is using a large layout, so show the fragment as a
        // dialog.
        newFragment.show(fragmentManager, "dialog")
    } else {
        // The device is smaller, so show the fragment fullscreen.
        val transaction = fragmentManager.beginTransaction()
        // For a polished look, specify a transition animation.
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        // To make it fullscreen, use the 'content' root view as the container
        // for the fragment, which is always the root view for the activity.
        transaction
                .add(android.R.id.content, newFragment)
                .addToBackStack(null)
                .commit()
    }
}

Java

public void showDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    CustomDialogFragment newFragment = new CustomDialogFragment();

    if (isLargeLayout) {
        // The device is using a large layout, so show the fragment as a
        // dialog.
        newFragment.show(fragmentManager, "dialog");
    } else {
        // The device is smaller, so show the fragment fullscreen.
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // For a polished look, specify a transition animation.
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        // To make it fullscreen, use the 'content' root view as the container
        // for the fragment, which is always the root view for the activity.
        transaction.add(android.R.id.content, newFragment)
                   .addToBackStack(null).commit();
    }
}

फ़्रैगमेंट ट्रांज़ैक्शन करने के बारे में ज़्यादा जानकारी के लिए, यहां देखें फ़्रैगमेंट.

इस उदाहरण में, mIsLargeLayout बूलियन से पता चलता है कि मौजूदा डिवाइस को ऐप्लिकेशन के बड़े लेआउट डिज़ाइन का इस्तेमाल करना होगा और इसलिए यह फ़्रैगमेंट को फ़ुल स्क्रीन के बजाय डायलॉग बॉक्स के तौर पर रखें. सबसे अच्छा तरीका यह सेट करने का बूलियन का मतलब है बूल रिसॉर्स वैल्यू वैकल्पिक संसाधन मान का इस्तेमाल करें. उदाहरण के लिए, यहां दो अलग-अलग स्क्रीन साइज़ के लिए बूल रिसॉर्स के वर्शन:

res/values/bools.xml

<!-- Default boolean values -->
<resources>
    <bool name="large_layout">false</bool>
</resources>

res/values-large/bools.xml

<!-- Large screen boolean values -->
<resources>
    <bool name="large_layout">true</bool>
</resources>

इसके बाद, mIsLargeLayout वैल्यू को शुरू करने के लिए, गतिविधि की onCreate() तरीका, जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है:

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    isLargeLayout = resources.getBoolean(R.bool.large_layout)
}

Java

boolean isLargeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    isLargeLayout = getResources().getBoolean(R.bool.large_layout);
}

बड़ी स्क्रीन पर, किसी गतिविधि को डायलॉग के तौर पर दिखाएं

छोटी स्क्रीन पर डायलॉग को फ़ुलस्क्रीन यूज़र इंटरफ़ेस (यूआई) के तौर पर दिखाने के बजाय, एक ही परिणाम के रूप में Activity को डायलॉग के रूप में बड़े स्तर पर दिखाया जाता है स्क्रीन. आपका चुना गया तरीका आपके ऐप्लिकेशन के डिज़ाइन पर निर्भर करता है. हालांकि, इसमें दिखाया गया है कि एक डायलॉग के तौर पर होने वाली गतिविधि अक्सर तब मददगार होती है, जब आपके ऐप्लिकेशन को छोटे स्क्रीन और आप टैबलेट पर एक डायलॉग के तौर पर, कम समय तक चलने वाली गतिविधि.

किसी गतिविधि को सिर्फ़ बड़ी स्क्रीन पर डायलॉग के तौर पर दिखाने के लिए, Theme.Holo.DialogWhenLarge अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है <activity> मेनिफ़ेस्ट एलिमेंट की थीम:

<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >

थीम के साथ अपनी गतिविधियों को शैली में ढालने के बारे में ज़्यादा जानकारी के लिए, देखें स्टाइल और थीम.

डायलॉग को ख़ारिज करना

जब उपयोगकर्ता किसी ऐसे ऐक्शन बटन पर टैप करता है जो AlertDialog.Builder, सिस्टम आपके लिए डायलॉग को खारिज कर देता है.

जब उपयोगकर्ता डायलॉग में किसी आइटम पर टैप करता है, तो सिस्टम डायलॉग को खारिज भी कर देता है सूची के अलावा, उस स्थिति में नहीं होगा जब सूची रेडियो बटन या चेकबॉक्स का इस्तेमाल करती हो. या फिर, आपके पास ये विकल्प हैं कॉल करके अपने डायलॉग बॉक्स को मैन्युअल तरीके से खारिज करें dismiss() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है आपके DialogFragment पर.

अगर डायलॉग बंद होने के बाद, आपको कुछ कार्रवाइयां करनी हैं, तो लागू करें onDismiss() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है तरीका है.DialogFragment

किसी डायलॉग को रद्द करने का विकल्प भी चुना जा सकता है. यह एक खास इवेंट है यह बताता है कि उपयोगकर्ता टास्क पूरा किए बिना डायलॉग छोड़ रहा है. यह तब होता है, जब उपयोगकर्ता 'वापस जाएं' बटन पर टैप करता है या डायलॉग के बाहर की स्क्रीन पर टैप करता है क्षेत्र है या अगर आप कॉल cancel() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है Dialog पर, जैसे कि "रद्द करें" के जवाब में बटन पर क्लिक करें डायलॉग.

जैसा कि पिछले उदाहरण में दिखाया गया है, रद्द करने के इवेंट का जवाब इस तरह से दिया जा सकता है: लागू करना onCancel() अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है आपकी DialogFragment क्लास में.