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


你可能感兴趣的:(Android小记)