当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发自定义弹框

Android开发自定义弹框

2020年08月23日  | 移动技术网移动技术  | 我要评论
我们如果想要实现这样的自定义弹框,需要怎么做呢,接下来就是教程首先,我们需要创建一个类继承Dialog下面就是代码package com.bw.movie;import android.annotation.SuppressLint;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.View;import android.widget..

在这里插入图片描述
我们如果想要实现这样的自定义弹框,需要怎么做呢,接下来就是教程
首先,我们需要创建一个类继承Dialog
下面就是代码

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

import androidx.annotation.NonNull;

/**
 * AUTHOR:XinLiang
 * DATE:2020/8/14
 * INTRODUCE:sdf$
 */
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.dialog_ding);
        //按空白处不能取消动画
        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);
        }
    }

    /**
     * 初始化界面控件
     */
    @SuppressLint("WrongViewCast")
    private void initView() {
        yes = (Button) findViewById(R.id.positiveTextView);
        no = (Button) findViewById(R.id.negativeTextView);
        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();
    }
}
在这里插入代码片 

布局文件必须对应,一下是布局文件的代码

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    android:layout_width="@dimen/dp_200"
    android:layout_height="wrap_content"
    android:layout_gravity="center"

    android:orientation="vertical">

    <ImageView
        android:layout_width="@dimen/dp_100"
        android:layout_height="@dimen/dp_100"
        android:layout_gravity="center"
        android:src="@drawable/dialog" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="自定义弹窗"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:visibility="visible" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">


        <TextView
            android:id="@+id/message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:gravity="left|center"
            android:lineSpacingMultiplier="1.5"
            android:paddingLeft="20.0dip"
            android:paddingTop="15.0dip"
            android:paddingRight="20.0dip"
            android:paddingBottom="15.0dip"
            android:text="确定要更新吗"
            android:textColor="#b8b8b8"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/negativeTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:text="取消"
            android:textColor="#b8b8b8"
            android:background="@drawable/yuanjian2"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/positiveTextView"
            android:layout_width="match_parent"

            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:text="确认"
            android:background="@drawable/btn_yuanjian"
            android:textColor="#fff"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout> 
如果对按钮有要求我们可以通过对shape的设置圆角等属性

接下来我给大家展示一下在values的styles里设置的属性

接下来就到最后一步,进行调用,在你需要调用的方法里设置
  CustomDialog.Builder builder = new CustomDialog.Builder(Main2Activity.this);
            builder.setMessage("确定要更新吗?");
            builder.setTitle("更新?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   	//确定的方法里不需要设置dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();//设置默认取消
                }
            });
                builder.create().show(); 

以上就是自定义弹框的全部过程,希望对你们有用

本文地址:https://blog.csdn.net/m0_48436034/article/details/108164819

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网