View.onClickListner不能从其他类中调用 [英] View.onClickListner not called from other class

查看:191
本文介绍了View.onClickListner不能从其他类中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着裁判我的previous问题在<一个href=\"http://stackoverflow.com/questions/10991805/button-not-shown-in-alertdialog#comment14360203_10991805\">button在alertDialog

With ref to my previous question in button not shown in alertDialog

我创建延伸AlertDialog类。在课堂上,我设置的XML有按钮的内容,但我的按钮没有响应。我的自定义警报的Java文件是

I am creating a class which extends AlertDialog. In the class I am setting the content from xml which has buttons, but my button is not responding. my custom alert java file is

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

            }
        });


    }

我的方法调用这个类是

My method calling this class is

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按钮没有被调用。
我不知道我在哪里得到wrong.Please帮助!

problem is my ok button is not called when clicked but cancel is called. I don't know where i am getting wrong.Please help!!!!

推荐答案

试试这个:

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

这篇关于View.onClickListner不能从其他类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆