2011-01-31 15 views
16

Tengo un botón y me gustaría abrir un cuadro de diálogo cuando lo presiono. Este es mi código:abrir un cuadro de diálogo cuando hago clic en un botón

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

¿Qué no funciona? – RoflcoptrException

+4

agrégalo a tu código alertDialog.show(); – ingsaurabh

+0

El constructor AlertDialog.Builder (nuevo View.OnClickListener() {}) no está definido –

Respuesta

32

Como ha dicho @Roflcoptr, no ha llamado al método alertDialog.show(). por lo tanto, su diálogo no aparece.

Aquí está tu código editado:

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! 
    } 

}); 

si se escribe en lugar de this<ActivityName>.this, entonces se va a tomar la referencia de View.OnClickListener desde this está accediendo actualmente en su interior. Debes dar el nombre de tu actividad allí.

+0

mi problema es que el eclipse supera al nuevo AlertDialog.Builder y dice que no está definido en onClick –

+1

AlertDialog alertDialog = new AlertDialog.Builder (ActivityName.this) .create(); – ingsaurabh

+3

durante la creación, simplemente no proporcione, ya que esto le da a myActivity. This ....... funcionará. –

-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(); 
Cuestiones relacionadas