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

Android में Alert Dialog कैसे दिखाएं

Alert Dialog क्या है?

Alert Dialog एक छोटा पॉप-अप विंडो है जो उपयोगकर्ता को निर्णय लेने के लिए प्रेरित करता है (जैसे "हाँ/नहीं", "ओके/कैंसिल")। यह वर्तमान एक्टिविटी के ऊपर दिखाई देता है और उपयोगकर्ता के इंटरैक्शन की प्रतीक्षा करता है।

AlertDialog.Builder के मुख्य मेथड्स

  • setTitle(CharSequence) - डायलॉग का शीर्षक सेट करता है
  • setMessage(CharSequence) - संदेश सामग्री सेट करता है
  • setIcon(int resId) - आइकन सेट करता है
  • setPositiveButton(text, listener) - सकारात्मक बटन (जैसे "हाँ")
  • setNegativeButton(text, listener) - नकारात्मक बटन (जैसे "नहीं")
  • setNeutralButton(text, listener) - तटस्थ बटन (जैसे "बाद में")
  • setView(View) - कस्टम लेआउट सेट करता है
  • setCancelable(boolean) - बैक बटन से बंद होने की अनुमति

उदाहरण: बेसिक Alert Dialog

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/showDialogBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="डायलॉग दिखाएं"
        android:layout_gravity="center" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        Button showDialogBtn = findViewById(R.id.showDialogBtn);
        showDialogBtn.setOnClickListener(v -> showAlertDialog());
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("पुष्टि करें");
        builder.setMessage("क्या आप वाकई बाहर निकलना चाहते हैं?");
        builder.setIcon(R.drawable.ic_warning);

        builder.setPositiveButton("हाँ", (dialog, which) -> {
            Toast.makeText(this, "हाँ पर क्लिक किया गया", Toast.LENGTH_SHORT).show();
            finish(); // एक्टिविटी बंद करें
        });

        builder.setNegativeButton("नहीं", (dialog, which) -> {
            Toast.makeText(this, "नहीं पर क्लिक किया गया", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        });

        builder.setNeutralButton("बाद में", (dialog, which) -> {
            dialog.dismiss();
        });

        builder.setCancelable(false); // बैक बटन से बंद न हो
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

कस्टम लेआउट के साथ Alert Dialog

// custom_dialog.xml लेआउट बनाएं
View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText input = customView.findViewById(R.id.inputField);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView)
       .setTitle("जानकारी दर्ज करें")
       .setPositiveButton("सबमिट", (dialog, which) -> {
           String text = input.getText().toString();
           // डेटा प्रोसेस करें
       })
       .setNegativeButton("रद्द", null)
       .show();

महत्वपूर्ण टिप्स

  • हमेशा AlertDialog.Builder का उपयोग करें, सीधे new AlertDialog() न बनाएं।
  • डायलॉग दिखाने के बाद dialog.dismiss() या dialog.cancel() का उपयोग करें।
  • लंबे ऑपरेशन के लिए ProgressDialog (डिप्रिकेटेड) के बजाय ProgressBar के साथ कस्टम डायलॉग उपयोग करें।
  1. Android में एक्टिविटी शुरू करने से पहले प्रोग्रेस डायलॉग कैसे दिखाएं? Android में एक्टिविटी शुरू करने से पहले प्रोग्रेस डायलॉग कैसे दिखाएं?

    Android में एक्टिविटी लॉन्च से पहले प्रोग्रेस डायलॉग दिखाना यह ट्यूटोरियल दिखाता है कि कैसे AsyncTask के साथ ProgressDialog का उपयोग करके बैकग्राउंड वर्क के दौरान लोडिंग इंडिकेटर दिखाया जाता है। नोट: ProgressDialog डिप्रिकेटेड है; आधुनिक ऐप्स में ProgressBar या Jetpack Compose का उपयोग करें। चरण 1:

  1. Android ऐप में नोटिफिकेशन काउंट कैसे दिखाएं? Android ऐप में नोटिफिकेशन काउंट कैसे दिखाएं?

    Android ऐप लॉन्चर आइकन पर नोटिफिकेशन बैज/काउंट दिखाना यह ट्यूटोरियल दिखाता है कि कैसे नोटिफिकेशन भेजते समय बैज काउंट सेट किया जाता है, जिससे लॉन्चर आइकन पर अनरीड काउंट दिखाई देता है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml

  1. एंड्रॉइड पर ऑलवेज-ऑन डिस्प्ले सक्रिय करें: त्वरित और आसान सेटअप एंड्रॉइड पर ऑलवेज-ऑन डिस्प्ले सक्रिय करें: त्वरित और आसान सेटअप

    एंड्रॉइड डिवाइस नई सुविधाओं के साथ आते रहते हैं जिनके बारे में हमने कभी नहीं सोचा था कि वे जरूरी हैं जब तक वे जारी नहीं किए गए। इस परंपरा को जारी रखते हुए, एंड्रॉइड ने ऑलवेज-ऑन फीचर पेश किया। हालांकि, यह शुरू में सैमसंग डिवाइस के लिए जारी किया गया था लेकिन अब यह अधिकांश एंड्रॉइड स्मार्टफोन पर आ गया