2011-01-31 10 views
16

Bir düğmem var ve bastığınızda bir iletişim kutusu açmak istiyorum. Bu benim kodunuz: AlertDialog#show demiyorlar çünküBir düğmeyi tıkladığımda bir iletişim kutusu aç

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

ne çalışmıyor? – RoflcoptrException

+4

bunu kod uyarınıza ekleyinDialog.show(); – ingsaurabh

+0

Yapıcı AlertDialog.Builder (yeni View.OnClickListener() {}) tanımlanmamış –

cevap

32

@Roflcoptr belirttiği gibi, sen alertDialog.show() yöntemi denir değil. böylece iletişim kutunuz görünmez. Eğer this yerine <ActivityName>.this, o zaman şu anda içine erişildiğini this beri View.OnClickListener referansını alacak yazarsanız

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

}); 

:

İşte Düzenlenen kod. Aktivitenin adını orada vermelisin.

+0

benim sorunum bu yeni alerttDialog.Builder hights ve onClick –

+1

AlertDialog alertDialog = undefined diyor yeni AlertDialog.Builder (ActivityName.this) .create(); – ingsaurabh

+3

sadece bunun yerine vermek vermeyin, bunun yerine myActivity.this ....... verin. –

10

Kişisel iletişim, gösterilmez.

+0

'da kullanımdan kaldırılmıştır. Senin için +1 dostum! –

-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(); 
İlgili konular