Android 自定义对话框

Android 自定义对话框


自定义对话框具体代码

package com.example.myapplication1227;

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 My extends Dialog {
    private TextView title;
    private TextView message;
    private Button no;
    private Button yes;

    private String titleStr;
    private String messageStr;
    private String yesStr;
    private String NoStr;

    private OnNoClickListener onNoClickListener;
    private OnYesClickListener onYesClickListener;

    //重写方法
    public My(Context context) {
        super(context);
    }

    //设置监听的接口
    public interface OnYesClickListener {

        void OnClick();

    }

    public interface OnNoClickListener {

        void OnClick();

    }

    //重写onCreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        title = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);

        if (titleStr != null) {
            title.setText(titleStr);
        }
        if (messageStr != null) {
            message.setText(messageStr);
        }
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (NoStr != null) {
            no.setText(NoStr);
        }
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onNoClickListener != null)
                    onNoClickListener.OnClick();
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onYesClickListener != null)
                    onYesClickListener.OnClick();

            }
        });
    }

    public void setTitleStr(String titleStr) {
        this.titleStr = titleStr;
    }

    public void setMessageStr(String messageStr) {
        this.messageStr = messageStr;
    }

    public void setYesStr(String yesStr) {
        this.yesStr = yesStr;
    }

    public void setNoStr(String noStr) {
        NoStr = noStr;
    }

    public void setOnNoClickListener(OnNoClickListener onNoClickListener) {
        this.onNoClickListener = onNoClickListener;
    }

    public void setOnYesClickListener(String str, OnYesClickListener onYesClickListener) {
        yesStr = str;
        this.onYesClickListener = onYesClickListener;
    }
}

自定义对话框的使用

package com.example.myapplication1227;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private Button but;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        but = (Button) findViewById(R.id.but);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //创建自定义对话框的实体类
                final My my = new My(Main2Activity.this);
                my.setTitleStr("asd");
                my.setMessageStr("123");
//                my.setNoStr("yes"); 设置按钮文字
//                my.setYesStr("no");

                my.setOnNoClickListener(new My.OnNoClickListener() {
                    @Override
                    public void OnClick() {
                        Toast.makeText(Main2Activity.this, "123", Toast.LENGTH_SHORT).show();
                        my.dismiss();
                    }
                });
                // 第一个参数 设置自定义按钮上的文字
                //自定义的监听器
                my.setOnYesClickListener("aaa",new My.OnYesClickListener() {
                    @Override
                    public void OnClick() {
                        Toast.makeText(Main2Activity.this, "456", Toast.LENGTH_SHORT).show();
                        my.dismiss();
                    }
                });
                //调用显示方法
                my.show();
            }
        });
    }
}

自定义对话框的布局

<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="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            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_gravity="center"
            android:layout_margin="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:lines="1"
                android:text="取消"
                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:lines="1"
                android:text="确定"
                android:textColor="#38ADFF"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值