Android Dialog使用

1、调用现有的确认弹窗

创建一个DialogInterface类型的监听器来监听弹窗,并设置在点击 positive按钮后执行的操作。然后创建一个现有的确定弹窗 AlertDialog.Builder bundle =
new AlertDialog.Builder(Each_Area_Mark.this);并设置其positive 按钮和negetive按钮的监听器。最后show出这个弹窗。bundle.show();

13、确认弹窗
delete.setOnClickListener(new View.OnClickListener() {   //删除按钮点击事件
                @Override
                public void onClick(View v) {
                    DialogInterface.OnClickListener onClickListener =
                            new DialogInterface.OnClickListener() {  //执行删除操作的监听器
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            int result = dao.deleteMark(listAllArea.get(position).getMark());
                            if(result == 0)
                            {
                                Toast.makeText(Each_Area_Mark.this, R.string.unableDeleteMark,
                                        Toast.LENGTH_SHORT).show();
                            }
                            else {
                                rightSqu -=listAllArea.get(position).getSquare() ;
                                info.setText(rightArea + "区 " + rightSqu + "/"
                                        + haveSquare.getAllSquare() + "亩");
                                listAllArea.remove(position);
                                //listAllArea = dao.getAllMark(rightArea);
                                markAdapter.notifyDataSetChanged();
                            }
                        }
                    };
                    System.out.println("delete");    //确认弹窗
                    AlertDialog.Builder bundle = new AlertDialog.Builder(Each_Area_Mark.this);
                    bundle.setTitle("确定要删除吗?");
                    bundle.setPositiveButton("确定",onClickListener); //确认 启动执行删除操作的监听器
                    bundle.setNegativeButton(“取消",null);
        bundle.show();
                }
            });

2、自定义Dialog

设计布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#11ffffff">

    <LinearLayout
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/free_dialog_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="15dp"
            android:gravity="center"
            android:text="消息提示"
            android:textColor="#38ADFF"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="提示消息" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="15dp"
            android:background="#E4E4E4" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/no"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="No"
                android:textColor="#7D7D7D"
                android:textSize="16sp" />

            <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4" />

            <Button
                android:id="@+id/yes"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="center"
                android:singleLine="true"
                android:text="Yes"
                android:textColor="#38ADFF"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

重构Dialog

package com.world.hello.selfdialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 创建自定义的dialog,主要学习其实现原理
 * Created by chengguo on 2016/3/22.
 */
public class SelfDialog extends Dialog {

    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView titleTv;//消息标题文本
    private TextView messageTv;//消息提示文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public SelfDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_exercise_sure_dialog_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后,向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后,向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            messageTv.setText(messageStr);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (noStr != null) {
            no.setText(noStr);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        titleTv = (TextView) findViewById(R.id.title);
        messageTv = (TextView) findViewById(R.id.message);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值