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

实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用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);
    }
}
布局文件如下:

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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android ,如果你想在 Activity 调用公共的自定义 Dialog,并使用确定和取消按钮,你可以按照以下步骤进行: 1. 首先,创建一个自定义Dialog ,该需要继承自 Dialog ,并实现确定和取消按钮的点击事件。例如: ``` public class MyDialog extends Dialog { private OnDialogButtonClickListener listener; public MyDialog(@NonNull Context context) { super(context); setContentView(R.layout.dialog_layout); Button okButton = findViewById(R.id.ok_button); Button cancelButton = findViewById(R.id.cancel_button); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (listener != null) { listener.onOkButtonClick(); } dismiss(); } }); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (listener != null) { listener.onCancelButtonClick(); } dismiss(); } }); } public void setOnDialogButtonClickListener(OnDialogButtonClickListener listener) { this.listener = listener; } public interface OnDialogButtonClickListener { void onOkButtonClick(); void onCancelButtonClick(); } } ``` 在这个例子,我们创建了一个 MyDialog ,该继承自 Dialog 实现了确定和取消按钮的点击事件,并提供了一个 OnDialogButtonClickListener 接口,用于在 Activity 监听确定和取消按钮的点击事件。 2. 然后,在 Activity 通过 LayoutInflater 加载自定义Dialog 布局,并创建 MyDialog 对象。例如: ``` LayoutInflater inflater = LayoutInflater.from(this); View layout = inflater.inflate(R.layout.dialog_layout, null); MyDialog dialog = new MyDialog(this); dialog.setContentView(layout); ``` 在这个例子,我们使用 LayoutInflater 加载了自定义Dialog 布局,并将其设置为 MyDialog 的内容视图。 3. 接下来,我们需要在 Activity 实现 OnDialogButtonClickListener 接口,以便监听 MyDialog 确定和取消按钮的点击事件。例如: ``` dialog.setOnDialogButtonClickListener(new MyDialog.OnDialogButtonClickListener() { @Override public void onOkButtonClick() { // 在这里处理确定按钮的点击事件 } @Override public void onCancelButtonClick() { // 在这里处理取消按钮的点击事件 } }); ``` 在这个例子,我们使用 setOnDialogButtonClickListener() 方法设置了 MyDialog 的 OnDialogButtonClickListener,以便在 Activity 监听确定和取消按钮的点击事件。 4. 最后,我们需要在 Activity 调用 MyDialog 的 show() 方法,显示自定义Dialog。例如: ``` dialog.show(); ``` 在这个例子,我们调用了 MyDialog 的 show() 方法,显示自定义Dialog。 注意,在 Activity 销毁时,需要调用 MyDialog 的 dismiss() 方法,以便释放 Dialog 相关的资源。例如: ``` @Override protected void onDestroy() { super.onDestroy(); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值