2012-06-12 42 views
2

調用裁判我剛纔的問題中button not shown in alertDialogView.onClickListner不能從其他類

我創建延伸AlertDialog類。在課堂上,我從xml中設置了具有按鈕的內容,但我的按鈕沒有響應。我的自定義警報的java文件

public class DateTimeDialog extends AlertDialog{ 

    Date date; 
    String title; 
    View.OnClickListener listner; 
    protected DateTimeDialog(Context context, String title, Date date) { 
     super(context, android.R.style.Theme_Holo_Light_Dialog); 
     // TODO Auto-generated constructor stub 
     this.title = title; 
     this.date = date; 
    } 

    public void initListener(View.OnClickListener listner){ 
     this.listner = listner; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     //super.onCreate(savedInstanceState); 
     setContentView(R.layout.date_time_picker); 

     setTitle(title); 

     Button dialogButtonOK = (Button) findViewById(R.id.btn_ok); 
     // if button is clicked, close the custom dialog 
     dialogButtonOK.setOnClickListener(listner); 

     Button dialogButtonCancel = (Button) findViewById(R.id.btn_cancel); 
     // if button is clicked, close the custom dialog 
     dialogButtonCancel.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View dialog) { 
       // TODO Auto-generated method stub 

      } 
     }); 


    } 

我的方法調用這個類是

final DateTimeDialog dateTimeDialog = new DateTimeDialog(context, "Title", date); 
      dateTimeDialog.show(); 
      dateTimeDialog.initListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        //done something 
       } 
      }); 

問題是點擊,但取消被稱爲當我的OK按鈕沒有被調用。 我不知道我在哪裏得到錯誤。請幫助!

+0

如果你聲明聽者的OK按鈕... – user370305

+0

要麼使它同一個cancelNutton或其他Java類做一個聽者吧.. – user370305

+0

,並從那裏的onclick處理。並在構造函數 – Android

回答

0

試試這個:

public void initListener(View.OnClickListener listner){ 
    this.listner = listner; 
    Button dialogButtonOK = (Button) findViewById(R.id.btn_ok); 
    dialogButtonOK.setOnClickListener(listner); 
} 
+0

好吧,謝謝你的回答 – Android