2011-01-31 14 views
16

Mam przycisk i chciałbym otworzyć okno dialogowe po naciśnięciu. To jest mój kod:Otwórz okno dialogowe po kliknięciu przycisku

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

Co nie działa? – RoflcoptrException

+4

dodaj to do swojego kodu alertDialog.show(); – ingsaurabh

+0

Konstruktor AlertDialog.Builder (new View.OnClickListener() {}) jest niezdefiniowany –

Odpowiedz

32

Jak powiedział @Roflcoptr, nie wywołałeś metody alertDialog.show(). w ten sposób twoje okno dialogowe się nie pojawi.

Oto kod Zmieniano:

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

}); 

jeśli piszesz this zamiast <ActivityName>.this, to zajmie odniesienie View.OnClickListener od this jest obecnie dostępna w środku. Musisz podać tam nazwę swojej działalności.

+0

Mój problem polega na tym, że zaćmienie zwiększa nowy AlertDialog.Builder i mówi, że jest niezdefiniowane w onClick –

+1

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

+3

podczas tworzenia po prostu nie dawaj, ponieważ to zamiast dać miActivity.this ....... to zadziała. –

-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(); 
Powiązane problemy