2012-08-28 5 views
7

Kendimi eğitimli yeni başlayan biri olduğum için sabır duyuyorum! Teşekkürler!OnClickListener içeride özel alertdialog Android

Eclipse'de kendi xml dosyasıyla ("custom_dialog") özel bir alertdialog yaptım ve buna "usernamealert" denir.

Kullanıcı henüz bir kullanıcı adı girmediyse (ör. Username.length == 0) pop-up uyarısı istiyorum.

Bu düzende bir textView (adınız nedir?), EditText ve button ("usernameButton") var.

Düğme için onclicklistener'ı yerleştirmeden önce, her şey çalıştı. Bu benim (ilgili) Java'mdı: (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(); 

Onclicklistener'ı yerleştirdiğimde, kırıldı! Onu nereye koymalıyım?

(şu ben denedim budur ... benim OnCreate tüm) dedim kod sonra

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



} 
}); 

: Yine

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

ben karıştırmasını başlamadan önce, işe yaradı buton!!

+0

Afte iletişim düzenini olan

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

hireteachernegotiate.xml bir düzene sahip Hireteachernegotiate.class, arama saatleri ve son olarak soruyu deftere naklediyorum, sadece 2 dakika sonra anladım! Tek yapmam gereken, düğmeyi nasıl bulduğunu değiştirmekti: Düğme usernameButton = (Düğme) usernamealert.findViewById (R.id.usernameButton); : Düğme usernameButton = (Düğme) dialoglayout.findViewById (R.id.usernameButton); // dialoglayout, View – delfina

+0

aradığınız her şeydir StackOverflow'a Hoş Geldiniz! Lütfen çözümünüzü bir cevap olarak gönderebilir ve izin verdikten sonra doğru cevap olarak kabul edebilir misiniz? Bu, gelecekte sorun yaşayan insanların aynı sorunu varsa, çözümü bulmasını kolaylaştıracaktır. – FoamyGuy

+0

cevabınız bana yardımcı oldu .. lütfen cevabınızı daha okunabilir hale getirin .. cevabımı aşağıda ekleyeceğim – mcr619619

cevap

2

Bunu deneyin. Bkz.

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

Bunun işe yarayıp yaramadığını ya da bir şekilde yardımcı olup olmadığına bakın.

+1

Bir ImageView üzerinde özel bir onClickListener eklediğimde bu iletişim kutusunu nasıl reddederim? – 4ndro1d

+0

@ 4ndro1d "dialog.dismiss();" kullanın. setOnClickListener() içindeki onClick yönteminin içinde; – AJW

7

kod arama düğmesi, onun sadece "findViewById" o konağın xml aramak istiyorsunuz, bu Bu benim sınıfının bir parçasıdır

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); 

olmalıdır eğer yeri belirtmek için gereklidir Bu

<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

iletişim düzeni adı "dialoghireteachernegotiate.xml" – mcr619619