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

एंड्रॉइड में डायलॉग के बाहर क्लिक करके डायलॉग कैसे डिसमिस करें?

एंड्रॉइड में AlertDialog को बाहर क्लिक करके बंद करना

यह ट्यूटोरियल दर्शाता है कि setCanceledOnTouchOutside(true) का उपयोग करके डायलॉग के बाहर टच करने पर उसे कैसे डिसमिस किया जाए।

लेआउट (activity_main.xml)

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="24dp">

    <TextView
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="डायलॉग दिखाने के लिए क्लिक करें"
        android:textSize="24sp" />
</LinearLayout>

MainActivity.java

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

public class MainActivity extends AppCompatActivity {
    private TextView textView;

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

        textView = findViewById(R.id.click);
        textView.setOnClickListener(v -> showAlertDialog());
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("पुष्टि");
        builder.setMessage("क्या आप इस डायलॉग को बंद करना चाहते हैं?");

        builder.setPositiveButton("हाँ", (dialog, which) -> 
            Toast.makeText(this, "आपने 'हाँ' पर क्लिक किया", Toast.LENGTH_SHORT).show());

        builder.setNegativeButton("नहीं", (dialog, which) -> 
            dialog.dismiss());

        AlertDialog dialog = builder.create();
        
        // महत्वपूर्ण: बाहर क्लिक करने पर डिसमिस इनेबल करें
        dialog.setCanceledOnTouchOutside(true);
        
        // वैकल्पिक: बैक बटन पर भी डिसमिस (डिफ़ॉल्ट true)
        dialog.setCancelable(true);
        
        dialog.show();
    }
}

महत्वपूर्ण मेथड्स

मेथड विवरण डिफ़ॉल्ट
setCanceledOnTouchOutside(true) डायलॉग के बाहर टच करने पर बंद करता है false
setCancelable(true) बैक बटन दबाने पर बंद करता है true

कस्टम डायलॉग (DialogFragment) के साथ

import android.app.Dialog;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;

public class CustomDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("कस्टम डायलॉग")
               .setMessage("यह एक DialogFragment है")
               .setPositiveButton("OK", null);
        
        Dialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }
}

// उपयोग:
new CustomDialogFragment().show(getSupportFragmentManager(), "custom_dialog");

कस्टम व्यू के साथ डायलॉग

View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
AlertDialog dialog = new AlertDialog.Builder(this)
    .setView(customView)
    .create();

dialog.setCanceledOnTouchOutside(true);

// कस्टम व्यू में बटन क्लिक
customView.findViewById(R.id.btn_close).setOnClickListener(v -> dialog.dismiss());

dialog.show();

नोट: फुल-स्क्रीन डायलॉग के लिए

यदि डायलॉग फुल-स्क्रीन है (उदाहरण के लिए, android:theme="@style/Theme.AppCompat.Dialog.NoActionBar" के साथ), तो बाहर कोई क्षेत्र नहीं होता, इसलिए setCanceledOnTouchOutside का कोई प्रभाव नहीं होगा।

  1. एंड्रॉइड में डायलॉग की स्क्रीन पोजीशन कैसे बदलें? एंड्रॉइड में डायलॉग की स्क्रीन पोजीशन कैसे बदलें?

    एंड्रॉइड में AlertDialog की पोजीशन सेट करना यह उदाहरण दिखाता है कि WindowManager.LayoutParams और Gravity का उपयोग करके डायलॉग को स्क्रीन पर किसी भी स्थान पर कैसे प्रदर्शित किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML (खा

  1. Android में डेटाबेस के साथ RecyclerView का उपयोग कैसे करें? Android में डेटाबेस के साथ RecyclerView का उपयोग कैसे करें?

    Android में SQLite डेटाबेस के साथ RecyclerView इम्प्लीमेंटेशन यह ट्यूटोरियल दिखाता है कि कैसे Room Database (आधुनिक) या SQLiteOpenHelper (लीगेसी) के साथ RecyclerView को जोड़ा जाता है, CRUD ऑपरेशन्स (बनाएं, पढ़ें, अपडेट करें, हटाएं) के साथ। आधुनिक दृष्टिकोण: Room Database (अनुशंसित) चरण 1: निर्भरता

  1. नेटफ्लिक्स कहीं भी देखें: नॉर्डवीपीएन का उपयोग US के अंदर और बाहर कैसे करें नेटफ्लिक्स कहीं भी देखें: नॉर्डवीपीएन का उपयोग US के अंदर और बाहर कैसे करें

    इसे पसंद करें या नफरत, लेकिन अपने टीवी और लैपटॉप की स्क्रीन से चिपके रहकर नेटफ्लिक्स देखना किसी न किसी तरह से हमारे लिए सबसे अच्छी थेरेपी है। फिल्मों से लेकर टीवी शो तक सामग्री की एक विस्तृत श्रृंखला स्ट्रीम करते हुए, नेटफ्लिक्स निस्संदेह सबसे मनोरंजक प्लेटफार्मों में से एक है। कोई आश्चर्य नहीं कि म