2011-01-31 16 views
16

पर क्लिक करता हूं तो एक संवाद खोलता है मेरे पास एक बटन होता है और दबाए जाने पर मैं एक संवाद खोलना चाहता हूं। यह मेरा कोड है:जब मैं एक बटन

Button more = (Button) findViewById(R.id.more); 
more.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     //Intent myIntent = new Intent(view.getContext(), agones.class); 
     //startActivityForResult(myIntent, 0); 

     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setTitle("hi"); 
     alertDialog.setMessage("this is my app"); 

     alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // here you can add functions 
     } 
     }); 
    } 
}); 
+0

क्या काम नहीं करता है? – RoflcoptrException

+4

इसे अपने कोड अलर्ट में जोड़ें Dialog.show(); – ingsaurabh

+0

कन्स्ट्रक्टर AlertDialog.builder (नया व्यू.ऑनक्लिक लिस्टनर() {}) अपरिभाषित –

उत्तर

32

जैसा कि @Roflcoptr ने कहा है, आपने alertDialog.show() विधि नहीं कहा है। इस प्रकार आपका संवाद प्रकट नहीं होता है।

Button more = (Button) findViewById(R.id.more); 
more.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     //Intent myIntent = new Intent(view.getContext(), agones.class); 
     //startActivityForResult(myIntent, 0); 


     AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update 
     alertDialog.setTitle("hi"); 
     alertDialog.setMessage("this is my app"); 

     alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // here you can add functions 
      } 
     }); 

     alertDialog.show(); //<-- See This! 
    } 

}); 

यदि आप लिखना this बजाय <ActivityName>.this, तो यह this के बाद से View.OnClickListener के संदर्भ लेने के लिए वर्तमान में यह अंदर ही ऐक्सेस किया जा रहा है जा रहा है:

यहाँ अपने संपादित कोड है। आपको वहां अपनी गतिविधि का नाम देना होगा।

+0

मेरी समस्या यह है कि ग्रहण नया अलर्टडिअलॉग.बिल्डर है और कहता है कि ऑनक्लिक –

+1

अलर्टडिअलॉग अलर्टडिअलॉग = नया अलर्टडिअलॉग.बिल्डर (ActivityName.this) .create() में अपरिभाषित है। – ingsaurabh

+3

बनाते समय इसे न दें बल्कि इसके बजाय मेरी गतिविधि दें। यह ....... यह काम करेगा। –

10

आपका संवाद, प्रदर्शित नहीं किया जाता है क्योंकि आप AlertDialog#show न बुलाया जाए।

+0

में बहिष्कृत किया गया है। आपके साथी के लिए +1! –

-2
  final AlertDialog.Builder builder = new AlertDialog.Builder(this); 

      builder.setMessage("this is message"); 
      builder.setTitle("this is title"); 

      //Setting message manually and performing action on button click 
      builder.setMessage("Do you want to close this application ?");T 
      //This will not allow to close dialogbox until user selects an option 
      builder.setCancelable(false); 
      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show(); 
          //builder.finish(); 
         } 
        }); 
      builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // Action for 'NO' Button 
          Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show(); 
          dialog.cancel(); 
         } 
        }); 

      //Creating dialog box 
      AlertDialog alert = builder.create(); 
      //Setting the title manually 
      //alert.setTitle("AlertDialogExample"); 
      alert.show(); 
संबंधित मुद्दे