覆盖Android中的Click侦听器方法

时间:2014-08-26 08:45:57

标签: android override customdialog

接口类:

    public interface CustomDialogMethods {

        public void btn_yes();
        public void btn_no();
    }

MainActivity从CustomDialogMethods实现它必须覆盖自定义按钮和取消方法;

    public class MainActivity extends Activity implements CustomDialogMethods {

    @Override
        public void btn_yes() {
            // TODO Auto-generated method stub

        }
    @Override
        public void btn_no() {
            // TODO Auto-generated method stub

        }
}

和我的自定义Dialog Class,它有okey和cancel按钮:

    public class CustomWorking extends Activity {

         void CustomDialogWorker(final Dialog dialog,int layout,int b_no,int b_yes) {

                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(layout);

                Button dialogButton = (Button) dialog.findViewById(b_yes);    
                Button dialogButtonno = (Button) dialog.findViewById(b_no);        

                dialogButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialogButtonno.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });          

                dialog.show();           
            }

使用接口方法覆盖MainActivity类中的对话框okey按钮和取消按钮的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

将您的上下文传递给CustomDialogWorker方法并触发btn_yes()btn_no()

CustomDialogWorker(dialog,layout,b_no,b_yes, this)

OR

MainActivity

中添加此代码
 listener = new CustomDialogMethods() {
        @Override
        public void btn_yes() {

        }

        @Override
        public void btn_no() {

        }
    };

传递监听器

CustomDialogWorker(dialog,layout,b_no,b_yes, listner);

在CustomDialogWorker方法中

触发方法

listner.btn_yes();
listener.btn_no();