Android 自定义Dialog类,并在Activity中实现按钮监听事件

实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下:

Android 自定义Dialog类,并在Activity中实现按钮监听事件_第1张图片

开发中遇到的问题无非在于如果在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);
    }
}
布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/night_biz_subscribe_media_recommend_item_bg"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/miaosu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="描述"
            android:textColor="@color/whitesmoke"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/myok"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="确定" />

            <Button
                android:id="@+id/myedit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="编辑" />
            
            <Button
                android:id="@+id/mysave"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="保存" />

            <Button
                android:id="@+id/mycancle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="取消" />
            
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

引用的style:

<style name="MyDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowBackground">@drawable/night_biz_subscribe_media_recommend_item_bg</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

最后在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();

如果想获得Dialog中的TextView控件可以这样获取,给TextView赋值时候一定要在Dialog show了之后在赋值,你懂得。

TextView text = (TextView) dialog.findViewById(R.id.miaosu);




你可能感兴趣的:(自定义dialog)