2012-08-28 33 views
7

Tôi là người mới bắt đầu tự học và đánh giá cao sự kiên nhẫn! Cảm ơn!OnClickListener bên trong cảnh báo tùy chỉnhdialog Android

Trong Eclipse, tôi đã tạo một customdialog tùy chỉnh với tệp xml của riêng nó ("custom_dialog") và nó được gọi là "usernamealert".

Tôi muốn cảnh báo bật lên nếu người dùng chưa nhập tên người dùng (ví dụ: username.length == 0).

Bên trong bố cục này, tôi có một textView ("Tên của bạn là gì?"), EditText và nút ("usernameButton").

Trước khi đặt onclicklistener cho nút, mọi thứ hoạt động. Đây là Java (có liên quan) của tôi:

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

Khi tôi đặt onclicklistener vào, nó đã hỏng! Tôi nên đặt nó ở đâu?

(sau đây là những gì tôi đã cố gắng ... tất cả trong OnCreate của tôi)

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



} 
}); 

Sau khi mã tôi đã nói:

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

Một lần nữa, nó đã làm việc trước khi tôi bắt đầu rối tung với nút !!

+0

afte r giờ tìm kiếm, và cuối cùng đăng câu hỏi, tôi chỉ tìm ra sau 2 phút! Tất cả những gì tôi phải làm khác nhau là thay đổi cách tìm nút: Tên người dùng nútButton = (Button) usernamealert.findViewById (R.id.usernameButton); cần có: Tên người dùng nútButton = (Button) dialoglayout.findViewById (R.id.usernameButton); // dialoglayout là bất cứ điều gì bạn gọi là View – delfina

+0

Chào mừng bạn đến với StackOverflow! Bạn có thể vui lòng gửi giải pháp của bạn như là một câu trả lời và chấp nhận nó như là câu trả lời đúng sau khi nó cho phép bạn. Điều đó sẽ giúp mọi người trong tương lai dễ dàng tìm ra giải pháp nếu họ có cùng một vấn đề. – FoamyGuy

+0

câu trả lời của bạn đã giúp tôi .. mặc dù hãy làm cho câu trả lời của bạn dễ đọc hơn .. im sẽ thêm câu trả lời của tôi dưới đây – mcr619619

Trả lời

2

Hãy thử điều này.

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

Hãy xem điều đó có hiệu quả hay không.

+1

Làm cách nào để loại bỏ hộp thoại này khi tôi thêm tùy chỉnh onClickListener trên ImageView? – 4ndro1d

+0

@ 4ndro1d Sử dụng "dialog.dismiss();" bên trong phương thức onClick nằm trong setOnClickListener(); Tên bố cục hộp thoại – AJW

7

Nó là cần thiết để xác định nơi sẽ tìm kiếm mã nút, nếu nó chỉ "findViewById" nó sẽ tìm kiếm trong xml của chủ nhà, nó phải là

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

Đây là một phần của lớp học của tôi, Hireteachernegotiate.class, trong đó có một bố trí của hireteachernegotiate.xml

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

Đây là cách bố trí thoại

<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

là "dialoghireteachernegotiate.xml" – mcr619619

Các vấn đề liên quan