2012-08-28 12 views
7

के अंदर ऑनक्लिक लिस्टनर मैं एक आत्म-सिखाया शुरुआती हूं और धैर्य की सराहना करता हूं! धन्यवाद!कस्टम अलर्टडियलॉग एंड्रॉइड

ग्रहण में, मैंने अपनी स्वयं की एक्सएमएल फ़ाइल ("custom_dialog") के साथ एक कस्टम अलर्टडियलॉग बनाया और इसे "उपयोगकर्ता नाम" कहा जाता है।

यदि उपयोगकर्ता ने अभी तक उपयोगकर्ता नाम दर्ज नहीं किया है (यानी उपयोगकर्ता नाम। Length == 0) तो मुझे पॉप-अप करने के लिए एक अलर्ट चाहिए।

इस लेआउट के अंदर मेरे पास एक टेक्स्ट व्यू ("आपका नाम क्या है?"), संपादन टेक्स्ट और बटन ("उपयोगकर्ता नाम बटन") है।

बटन के लिए ऑनक्लिकलिस्टर डालने से पहले, सबकुछ काम करता था। यह मेरा (प्रासंगिक) जावा था:

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)  getCurrentFocus()); 
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this); 
usernamebuilder.setView(dialoglayout); 

AlertDialog usernamealert = usernamebuilder.create(); 

जब मैंने ऑनक्लिकलिस्टर को रखा, तो यह टूट गया! मुझे इसे कहां रखना चाहिए?

(निम्नलिखित मैं क्या कोशिश की थी है ... मेरी OnCreate में सभी)

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus()); 
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this); 
usernamebuilder.setView(dialoglayout); 


Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton); 
usernameButton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
//store username in sharedprefs 
usernamealert.dismiss(); 



} 
}); 

कोड मैंने कहा के बाद:

if (username.length() == 0) { 
      usernamealert.show(); 
     } 

फिर, यह काम किया इससे पहले कि मैं के साथ खिलवाड़ करना शुरू कर दिया बटन !!

+0

Afte है खोज के आर घंटे, और आखिरकार सवाल पोस्ट करते हुए, मैंने बस 2 मिनट बाद इसे समझ लिया! मुझे बस इतना करना चाहिए था कि यह बटन कैसे मिला: बटन उपयोगकर्ता नामबटन = (बटन) usernamealert.findViewById (R.id.usernameButton); यह होना चाहिए था: बटन उपयोगकर्ता नामबटन = (बटन) dialoglayout.findViewById (R.id.usernameButton); // डायलॉगआउट जो भी आपने देखा है – delfina

+0

StackOverflow में आपका स्वागत है! क्या आप कृपया अपना समाधान उत्तर के रूप में पोस्ट कर सकते हैं और इसे सही उत्तर के रूप में स्वीकार कर सकते हैं जब यह आपको अनुमति देता है। इससे भविष्य में लोगों को समाधान मिलना आसान हो जाएगा यदि उन्हें एक ही समस्या है। – FoamyGuy

+0

आपके उत्तर ने मेरी मदद की .. हालांकि कृपया अपना उत्तर अधिक पठनीय बनाएं .. मैं – mcr619619

उत्तर

2

इसे आजमाएं।

usernamebuilder.setCancelable(false) 
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      //do what you want. 
     } 
    }); 

देखें कि क्या यह काम करता है, या यदि यह किसी भी तरह से मदद करता है।

+1

से नीचे अपना उत्तर जोड़ने जा रहा हूं, जब मैं एक छवि दृश्य पर कस्टम ऑनक्लिक लिस्टनर जोड़ता हूं तो मैं इस संवाद को कैसे खारिज कर सकता हूं? – 4ndro1d

+0

@ 4ndro1d "dialog.dismiss();" का प्रयोग करें onClick विधि के अंदर जो setOnClickListener() के भीतर है; – AJW

7

यह है, जहां कोड खोज बटन है, अगर इसकी केवल "findViewById" यह मेजबान का एक्सएमएल में खोज करेंगे, यह होना चाहिए होगा

LayoutInflater inflater =getLayoutInflater(); 
       View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null); 
       // Inflate and set the layout for the dialog 
       // Pass null as the parent view because its going in the dialog layout 
       builder.setView(myview); 
       Button addS = (Button) myview.findViewById (R.id.bAddS); 

यह मेरा वर्ग का हिस्सा है निर्दिष्ट करने के लिए की जरूरत है Hireteachernegotiate.class, जो hireteachernegotiate.xml के लेआउट

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       // Get the layout inflater 
       LayoutInflater inflater =getLayoutInflater(); 
       View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null); 
       // Inflate and set the layout for the dialog 
       // Pass null as the parent view because its going in the dialog layout 
       builder.setView(myview); 

       Button addS = (Button) myview.findViewById (R.id.bAddS); 
       addS.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         //do some stuff 
        } 
       }); 

       Button minusS = (Button) myview.findViewById (R.id.bMinusS); 
       addS.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         //do other stuff 
        } 
       }); 

       // Add action buttons 
         builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 


           dialog.cancel(); 
          } 
         }); 
        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 



      builder.show(); 

यह संवाद लेआउट

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button 
     android:id="@+id/bAddS" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 


    <Button 
     android:id="@+id/bMinusS" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+0

संवाद लेआउट नाम "dialoghireteachernegotiate.xml" है – mcr619619

संबंधित मुद्दे