AlertDialog.Builder कस्टम लेआउट और EditText के साथ; पहुँच नहीं सकते


101

मैं एक EditTextवस्तु के साथ एक चेतावनी संवाद बनाने की कोशिश कर रहा हूं । मुझे EditTextप्रोग्राम के प्रारंभिक पाठ को सेट करने की आवश्यकता है । यहाँ मेरे पास क्या है।

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null));
EditText editText = (EditText) findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

मुझे क्या बदलने की आवश्यकता है ताकि मेरे पास एक वैध EditTextवस्तु हो सके?

[संपादित करें]

इसलिए, यह user370305 और अन्य लोगों द्वारा इंगित किया गया था जिनका मैं उपयोग कर रहा हूं alertDialog.findViewById(R.id.label_field);

दुर्भाग्य से यहाँ एक और मुद्दा है। जाहिर तौर पर, AlertDialogरनटाइम पर प्रोग्राम क्रैश होने के कारणों पर कंटेंट व्यू सेट करना । आपको इसे बिल्डर पर सेट करना होगा।

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

दुर्भाग्य से, जब आप ऐसा करते हैं, तो alertDialog.findViewById(R.id.label_field);अब वापस आ जाता है null

[/ संपादित करें]

जवाबों:


238

editTextalertDialogलेआउट का एक हिस्सा है, इसलिए केवल editTextसंदर्भ के साथ उपयोग करेंalertDialog

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);

अपडेट करें:

क्योंकि कोड लाइन में dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterहै अशक्त

अपने कोड को नीचे की तरह अपडेट करें, और प्रत्येक कोड लाइन को समझने की कोशिश करें

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

अपडेट 2:

जैसा कि आप यूआई घटकों को अपडेट करने के लिए इन्फ्लेटर द्वारा बनाई गई व्यू ऑब्जेक्ट का उपयोग कर रहे हैं, अन्यथा आप सीधे कक्षा की setView(int layourResId)विधि का उपयोग कर सकते हैं AlertDialog.Builder, जो कि एपीआई 21 और उसके बाद से उपलब्ध है।


22
U इसे भी कर सकते हैं:dialogBuilder.setView(R.layout.dialog_layout);
सियावा

4
@ सियावा यह विधि केवल फॉर्म 21 एपीआई उपलब्ध है।
स्कारक्स

मैं डायलॉग प्रदर्शित करने की कोशिश कर रहा हूं और यह RecyclerView में काम नहीं कर रहा था, लेकिन इसने किया।
मुनीब मिर्जा

getLayoutInflater()जब आप inflaterपरिभाषित नहीं है तो आप इसका उपयोग कर सकते हैं ।
बैकस्लैश

1
@saigopi जब आप onClick पर ओवरराइड करते हैं, तो तर्क होंगे (DialogInterface डायलॉग, int id)। इस ऑनक्लिक विधि में, बस डायलॉग।
मिंकू

28

इस एक का उपयोग करें

   AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    // Get the layout inflater
    LayoutInflater inflater = (activity).getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the
    // dialog layout
    builder.setTitle(title);
    builder.setCancelable(false);
    builder.setIcon(R.drawable.galleryalart);
    builder.setView(inflater.inflate(R.layout.dialogue, null))
    // Add action buttons
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    }
                }
            });
    builder.create();
    builder.show();

होना चाहिए builder.create().show();, आप builder.show();अधिक विवरण के लिए कोड की जांच कर सकते हैं
फान वान लिन्ह

9

तुम लिख सकते हो:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

 // ...Irrelevant code for customizing the buttons and title

LayoutInflater inflater = this.getLayoutInflater(); 

View dialogView= inflater.inflate(R.layout.alert_label_editor, null);                    
dialogBuilder.setView(dialogView);

Button button = (Button)dialogView.findViewById(R.id.btnName);

   button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

         //Commond here......

       }
   });

EditText editText = (EditText)
dialogView.findViewById(R.id.label_field); 

editText.setText("test label"); 

dialogBuilder.create().show();

3

मामले में कोई भी इसे कोटलिन में चाहता है:

val dialogBuilder = AlertDialog.Builder(this)
// ...Irrelevant code for customizing the buttons and title
val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
dialogBuilder.setView(dialogView)

val editText =  dialogView.findViewById(R.id.label_field)
editText.setText("test label")
val alertDialog = dialogBuilder.create()
alertDialog.show()

फिर से पोस्ट किया @ user370305 के जवाब।




1
/**
 * Shows  confirmation dialog about signing in.
 */
private void startAuthDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    alertDialog.getWindow().setLayout(800, 1400);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.auth_dialog, null);
    alertDialog.getWindow().setContentView(dialogView);
    EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
    editText.setText("test label");
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.