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

Android पर कस्टम डायलॉग बॉक्स कैसे बनाएं

परिचय

एक कस्टम डायलॉग उपयोगकर्ता इनपुट लेने या कस्टम लेआउट के साथ जानकारी प्रदर्शित करने की अनुमति देता है। यह लेख `AlertDialog.Builder` और एक कस्टम XML लेआउट का उपयोग करके एक कस्टम डायलॉग बनाने का तरीका दिखाता है।

चरण-दर-चरण कार्यान्वयन

चरण 1: मुख्य लेआउट (activity_main.xml)

<Button 
    android:id="@+id/btn_show" 
    android:layout_width="140dp" 
    android:layout_height="80dp" 
    android:layout_centerInParent="true" 
    android:text="Show Message" 
    android:onClick="btn_showMessage" />

चरण 2: कस्टम डायलॉग लेआउट (custom_dialog.xml)

रिसोर्स → लेआउट → नया → लेआउट रिसोर्स फाइल → नाम: `custom_dialog`, रूट: `LinearLayout`

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="15dp"> 
    <TextView android:text="Enter your Message" android:textSize="22sp" android:gravity="center" ... /> 
    <EditText android:id="@+id/txt_input" android:hint="Enter your Message" android:textSize="20sp" ... /> 
    <LinearLayout android:orientation="horizontal" ...> 
        <Button android:id="@+id/btn_cancel" android:text="Cancel" android:layout_weight="1" ... /> 
        <Button android:id="@+id/btn_okay" android:text="Okay" android:layout_weight="1" ... /> 
    </LinearLayout> 
</LinearLayout>

चरण 3: MainActivity.java

public class MainActivity extends AppCompatActivity { 
    TextView myCustomMessage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        myCustomMessage = findViewById(R.id.myCustommessage); 
    } 

    public void btn_showMessage(View view) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null); 
        EditText input = dialogView.findViewById(R.id.txt_input); 
        Button cancel = dialogView.findViewById(R.id.btn_cancel); 
        Button okay = dialogView.findViewById(R.id.btn_okay); 

        builder.setView(dialogView); 
        AlertDialog dialog = builder.create(); 
        dialog.setCanceledOnTouchOutside(false); 

        cancel.setOnClickListener(v -> dialog.dismiss()); 
        okay.setOnClickListener(v -> { 
            myCustomMessage.setText(input.getText().toString()); 
            dialog.dismiss(); 
        }); 

        dialog.show(); 
    } 
}

व्याख्या

  • `LayoutInflater` कस्टम XML को व्यू में फुलाता है।
  • `AlertDialog.Builder.setView()` कस्टम व्यू सेट करता है।
  • बटनों के क्लिक लिसनर डायलॉग को खारिज करते हैं या इनपुट को मुख्य टेक्स्टव्यू में सेट करते हैं।
  1. एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं? एंड्रॉइड में कस्टम रेटिंग बार कैसे बनाएं?

    एंड्रॉइड में कस्टम RatingBar बनाना — ड्रॉएबल, स्टाइल, और प्रोग्रामेटिक कंट्रोल एंड्रॉइड का डिफ़ॉल्ट RatingBar सितारे दिखाता है, लेकिन इसका लुक कस्टमाइज़ करना (रंग, आइकन, आकार, एनिमेशन) आम जरूरत है। यह ट्यूटोरियल **XML स्टाइलिंग** और **प्रोग्रामेटिक कंट्रोल** दोनों दिखाता है। चरण 1: नया प्रोजेक्ट बन

  1. एंड्रॉइड में एक्शन बार में कस्टम व्यू कैसे प्रदर्शित करें? एंड्रॉइड में एक्शन बार में कस्टम व्यू कैसे प्रदर्शित करें?

    कस्टम लेआउट के साथ एक्शन बार पर्सनलाइज़ करें यह उदाहरण दर्शाता है कि कस्टम लेआउट फाइल का उपयोग करके एक्शन बार में कस्टम व्यू (लोगो, शीर्षक, आइकन) कैसे सेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मेन लेआउट res/layout/activity_main.xml - साधारण लेआउट। चर

  1. आउटलुक में कस्टम व्यू कैसे बनाएं आउटलुक में कस्टम व्यू कैसे बनाएं

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