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

एंड्रॉइड में बिना शीर्षक वाला डायलॉग बॉक्स कैसे बनाएं?

एंड्रॉइड में शीर्षक रहित डायलॉग बॉक्स बनाना

यह उदाहरण दिखाता है कि AlertDialog का उपयोग करके बिना शीर्षक वाला डायलॉग कैसे बनाया जाता है।

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

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:text="AlertDialog डेमो" />

    <Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText"
        android:text="डेटा साफ़ करें" />

</RelativeLayout>

चरण 3: MainActivity में डायलॉग लॉजिक

MainActivity.java:

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

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button btnClear;

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

        editText = findViewById(R.id.editText);
        btnClear = findViewById(R.id.btnClear);

        btnClear.setOnClickListener(v -> showConfirmDialog());
    }

    private void showConfirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // शीर्षक सेट न करके हम शीर्षक रहित डायलॉग बनाते हैं
        // builder.setTitle("पुष्टि"); // यह लाइन हटाएं या कमेंट करें
        
        builder.setMessage("क्या आप टेक्स्ट साफ़ करना चाहते हैं?");
        
        builder.setPositiveButton("हाँ", (dialog, which) -> {
            editText.setText("");
            Toast.makeText(this, "टेक्स्ट साफ़ हो गया", Toast.LENGTH_SHORT).show();
        });

        builder.setNegativeButton("रद्द", (dialog, which) -> dialog.dismiss());

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

बिना शीर्षक के डायलॉग के लिए अतिरिक्त विकल्प

यदि आप कस्टम लेआउट के साथ पूरी तरह से शीर्षक रहित डायलॉग चाहते हैं:

// कस्टम लेआउट के साथ
View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // कोई शीर्षक सेट न करें
AlertDialog dialog = builder.create();
dialog.show();

चरण 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। "डेटा साफ़ करें" बटन दबाने पर बिना शीर्षक वाला पुष्टि डायलॉग दिखाई देगा।

  1. Android में पेजिनेटिंग टेक्स्ट कैसे बनाएं? Android में पेजिनेटिंग टेक्स्ट कैसे बनाएं?

    यह उदाहरण दर्शाता है कि लंबे टेक्स्ट को पेज में बांटकर (पेजिनेशन) कैसे दिखाया जाए, जैसे ई-बुक रीडर ऐप्स में होता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml - नेविगेशन बटन्स के साथ TextView: <?xml version=1.0 encoding=utf-

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

    एंड्रॉइड में AlertDialog को बाहर क्लिक करके बंद करना यह ट्यूटोरियल दर्शाता है कि setCanceledOnTouchOutside(true) का उपयोग करके डायलॉग के बाहर टच करने पर उसे कैसे डिसमिस किया जाए। लेआउट (activity_main.xml) <LinearLayout xmlns:android=https://schemas.android.com/apk/res/android android:layout_

  1. गूगल API के बिना एंड्रॉइड में स्पीच रिकग्नाइज़र कैसे विकसित करें? गूगल API के बिना एंड्रॉइड में स्पीच रिकग्नाइज़र कैसे विकसित करें?

    एंड्रॉइड में ऑन-डिवाइस स्पीच रिकग्निशन (गूगल API के बिना) यह ट्यूटोरियल SpeechRecognizer API का उपयोग करके ऑन-डिवाइस स्पीच रिकग्निशन लागू करने का तरीका दिखाता है, जो इंटरनेट के बिना काम करता है (यदि डिवाइस ऑफलाइन मॉडल सपोर्ट करता है)। आवश्यक अनुमतियाँ <uses-permission android:name=android.permis