Android自定义DiaLog对话框

常用的对话框

对话框特殊
普通AlertDialog.Builder()
单选AlertDialog.Builder()setSingleChoiceItems()
多选AlertDialog.Builder()setMultiChoiceItems()
日期DatePickerDialognew DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day);
时间TimePickerDialognew TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制);
进度条ProgressDialogsetStyle(ProgressDialog.STYLE_HORIZONTAL)
自定义AlertDialog.Builder()setView()

自定义对话框

在这里插入图片描述

思路
1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义类的对象并展示

代码:

(1)xml布局:自定义对话框的布局

<?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">

    <LinearLayout
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/a"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#12B6FF"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="评价一下吧!"
                android:textColor="#fff"
                android:textSize="25sp"
                android:textStyle="bold" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#fff"
            android:orientation="vertical">

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:gravity="center"
                android:text="喜欢吗?给个五星好评, 鼓励\n我们变得更好"
                android:textSize="20sp"
                android:textStyle="bold" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/no"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:text="我要吐槽" />

                <Button
                    android:id="@+id/yes"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:text="五星好评"
                    android:textColor="#12B6FF" />

            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

(2)自定义类继承dialog

public class dialog extends Dialog {
    private TextView title;    //标题
    private TextView message;  //内容
    private Button no;         //取消按钮
    private Button yes;        //确认按钮
    private String stitle, smessage;         //自定义标题和内容信息
    YesOnclickListener yesOnclickListener;     //确认接口监听
    NoOnclickListener noOnclickListener;        //取消接口监听

    public interface YesOnclickListener {      //确认接口
        void yesclick();
    }

    public interface NoOnclickListener {       //取消接口
        void noclick();
    }


    //通过set方法从外界设置标题和内容以及按钮监听
    public void setStitle(String stitle) {
        this.stitle = stitle;
    }

    public void setSmessage(String smessage) {
        this.smessage = smessage;
    }

    public void setYesOnclickListener(YesOnclickListener yesOnclickListener) {
        this.yesOnclickListener = yesOnclickListener;
    }

    public void setNoOnclickListener(NoOnclickListener noOnclickListener) {
        this.noOnclickListener = noOnclickListener;
    }

    public dialog(@NonNull Context context) {
        super(context);
    }  //构造


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ff);  //加载自定义布局(对话框)
        initView(); //初始化控件

        //外界设置标题和内容到控件
        if (message != null) {
            message.setText(smessage);
        }
        if (title != null) {
            title.setText(stitle);
        }

        yes.setOnClickListener(new View.OnClickListener() {     //确认点击事件
            @Override
            public void onClick(View view) {
                yesOnclickListener.yesclick();  //接口中的方法
                dismiss();
            }
        });
        no.setOnClickListener(new View.OnClickListener() {     //取消点击事件
            @Override
            public void onClick(View view) {
                noOnclickListener.noclick();//接口中的方法
                dismiss();
            }
        });
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);
    }
}

(3)Activity点击展示对话框

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    public void onclick(View view) {
        dialog dialog = new dialog(this);       //创建对象

        dialog.setYesOnclickListener(new dialog.YesOnclickListener() {                //实现确认按钮
            @Override
            public void yesclick() {
                Toast.makeText(Main2Activity.this, "给了五星好评", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setNoOnclickListener(new dialog.NoOnclickListener() {            //实现取消按钮
            @Override
            public void noclick() {
                Toast.makeText(Main2Activity.this, "吐槽了一下", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setSmessage("喜欢吗?给个五星好评, 鼓励\n我们变得更好");     //设置自定义内容
        dialog.setStitle("评价一下吧");      //设置自定义标题
        dialog.show(); //展示

    }
}

效果:
在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值