बाहरी टच पर AlertDialog खारिज करना
डिफ़ॉल्ट रूप से, AlertDialog बाहर टैप करने पर खारिज हो जाता है। यह उदाहरण दिखाता है कि व्यवहार को कैसे नियंत्रित किया जाए—इसे सक्षम/अक्षम करना और कस्टम लेआउट को सही ढंग से संभालना।
त्वरित उत्तर
AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(true); // बाहर टैप पर खारिज करने की अनुमति दें (डिफ़ॉल्ट) dialog.show();
false पर सेट करने से बाहरी टच खारिज करना अक्षम हो जाता है; उपयोगकर्ता को एक बटन दबाना होगा।
पूर्ण कार्यान्वयन उदाहरण
लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="डायलॉग दिखाएं" />
<CheckBox
android:id="@+id/chkOutsideCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="बाहरी टच पर खारिज करने की अनुमति दें"
android:checked="true" />
</LinearLayout>
गतिविधि (MainActivity.java)
package com.example.dialogdismiss;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
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 btnShow = findViewById(R.id.btnShowDialog);
CheckBox chkOutside = findViewById(R.id.chkOutsideCancel);
btnShow.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("नमूना डायलॉग");
builder.setMessage("बाहर टैप करके मुझे खारिज करें — या नहीं, चेकबॉक्स के आधार पर।");
builder.setPositiveButton("ओके", (DialogInterface d, int which) ->
Toast.makeText(this, "पॉजिटिव क्लिक किया गया", Toast.LENGTH_SHORT).show());
builder.setNegativeButton("रद्द करें", null);
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(chkOutside.isChecked());
dialog.show();
});
}
}
महत्वपूर्ण नोट्स
setCanceledOnTouchOutside(true)डायलॉग विंडो के बाहर टच के लिए काम करता है; डायलॉग के अंदर टच (बटन, टेक्स्ट) इसे प्रभावित नहीं करते।- यदि आप
setCancelable(false)सेट करते हैं, तो बैक बटन **और** बाहरी टच दोनों अक्षम हो जाते हैं। व्यक्तिगत रूप से नियंत्रित करने के लिए दोनों विधियों का उपयोग करें। - कस्टम डायलॉग लेआउट (
setView()) के लिए, वही नियम लागू होते हैं—सुनिश्चित करें कि डायलॉग विंडो में पैडिंग/मार्जिन है ताकि "बाहर" क्षेत्र मौजूद हो।
परीक्षण
- ऐप चलाएं, चेकबॉक्स टॉगल करें, "डायलॉग दिखाएं" दबाएं।
- चेक किया गया → डायलॉग के बाहर टैप करें → डायलॉग खारिज हो जाता है।
- अनचेक किया गया → बाहर टैप करें → कुछ नहीं होता; बटन का उपयोग करना होगा।
RecyclerView/बॉटम शीट के अंदर डायलॉग के लिए
यदि डायलॉग एक बॉटम शीट या अन्य कंटेनर के अंदर है, तो सुनिश्चित करें कि पैरेंट व्यू android:clickable="true" के साथ टच इवेंट को इंटरसेप्ट नहीं करता है, अन्यथा बाहरी टच डायलॉग तक नहीं पहुंचेगा।