Android 自定义Dialog,自定义接口监听事件。

开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的按钮,Dialog类如下,在MyDialog这个类中实现了一个LeaveMyDialogListener接口,用来实现onclick的点击事件

package com.Ieasy.Tool;
import com.Ieasy.ieasyware.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog implements android.view.View.OnClickListener {

    private Context context;
    private TextView txt;
    private Button   btnok,btnedit,btncancle,btnsave;
    private LeaveMyDialogListener listener;
    
    public interface LeaveMyDialogListener{   
        public void onClick(View view);   
    }   
    
    public MyDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }
    
    public MyDialog(Context context,int theme,LeaveMyDialogListener listener) {
        super(context,theme);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(com.Ieasy.ieasyware.R.layout.mydialog);
        btncancle = (Button)findViewById(R.id.mycancle);
        btnedit   = (Button)findViewById(R.id.myedit);
        btnok     = (Button)findViewById(R.id.myok);
        txt       = (TextView)findViewById(R.id.miaosu);
        btnsave   = (Button)findViewById(R.id.mysave);
        btncancle.setOnClickListener(this);
        btnedit.setOnClickListener(this);
        btnok.setOnClickListener(this);
        btnsave.setOnClickListener(this);
    }

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

Activity中调用,通过LeaveMyDialogListener 接口来实现在Activity中的点击事件

MyDialog dialog = new MyDialog(context,R.style.MyDialog,   
        new MyDialog.LeaveMyDialogListener() {   
         @Override   
         public void onClick(View view) {   
            switch(view.getId()){   
               case R.id.myok:
                        break;
              case R.id.myedit:

              break;
              case R.id.mycancle:
                       
              break;
              case R.id.mysave:
              dialog.dismiss();
                         
              default:
              break;
                     }   
                 }   
            });   
            dialog.show();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值