安卓自定义AlertDialog弹出框

SystemAlertDialogManager 类

public class SystemAlertDialogManager : Activity
    {
        private Dialog mDialog;
        private Button mYes;
        private Button mNo;
        private Context mContext;
        private TextView tvTitle;
        public SystemAlertDialogManager(Context context)
        {
            this.mContext = context;
        }
        public void ShowRecordingDialog(string remind, string yes, string no)
        {
            this.mDialog = new Dialog(mContext, Resource.Style.SystemAlertDialog);
            this.mDialog.RequestWindowFeature((int)WindowFeatures.NoTitle);
            LayoutInflater inflate = LayoutInflater.From(mContext);
            View view = inflate.Inflate(Resource.Layout.dialog_confirm, null);
            mDialog.SetContentView(view);
            this.mYes = view.FindViewById<Button>(Resource.Id.btnSure);
            this.mNo = view.FindViewById<Button>(Resource.Id.btnCancel);
            this.tvTitle = view.FindViewById<TextView>(Resource.Id.tvTitle);
            tvTitle.Text = remind;
            mYes.Text = yes;
            mNo.Text = no;
            mYes.Click += mYes_Click;
            mNo.Click += mNo_Click;
            mDialog.Show();
        }

        void mNo_Click(object sender, EventArgs e)
        {
            if (NoClicked != null)
            {
                NoClicked(mContext, e);
            }
        }

        void mYes_Click(object sender, EventArgs e)
        {
            if (YesClicked != null)
            {
                YesClicked(mContext, e);
            }
        }
        public void CloseDialog()
        {
            this.mDialog.Dismiss();
            this.mDialog.Dispose();
            this.mDialog = null;
        }
        public event EventHandler<EventArgs> YesClicked;
        public event EventHandler<EventArgs> NoClicked;
    }

style样式

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="SystemAlertDialog" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
  </style>
</resources>

dialog_confirm

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="@dimen/dialog_w"
    android:layout_height="@dimen/dialog_h"
    android:background="@drawable/dialog_bg"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="标题"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dialog_title_mt"
        android:textSize="@dimen/dialog_title_font"
        android:textColor="@android:color/white"
        android:paddingBottom="@dimen/dialog_title_mb"
        android:gravity="center"
        android:id="@+id/tvTitle" />
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/linearLayout1">
        <Button
            android:text="确定"
            android:layout_width="@dimen/dialog_btn_w"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dialog_button_font"
            android:textColor="@drawable/dialog_btn_selector"
            android:paddingTop="@dimen/dialog_button_ptb"
            android:paddingBottom="@dimen/dialog_button_ptb"
            android:gravity="center"
            android:background="@android:color/transparent"
            android:id="@+id/btnSure" />
        <Button
            android:text="取消"
            android:layout_width="@dimen/dialog_btn_w"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:textSize="@dimen/dialog_button_font"
            android:textColor="@drawable/dialog_btn_selector"
            android:paddingTop="@dimen/dialog_button_ptb"
            android:paddingBottom="@dimen/dialog_button_ptb"
            android:gravity="center"
            android:id="@+id/btnCancel" />
    </LinearLayout>
</LinearLayout>

界面中使用

var msg = new SystemAlertDialogManager(this);
            msg.ShowRecordingDialog("是否删除数据", "确定", "取消");
            msg.YesClicked += new EventHandler<EventArgs>((a, b) =>
            {
                msg.CloseDialog();
                //这里点击确定以后要执行的操作
            });
            msg.NoClicked += new EventHandler<EventArgs>((a, b) =>
            {
                msg.CloseDialog();
            });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值