自定义对话框

◇◆丶佛笑我妖孽 提交于 2019-12-28 01:31:37

自定义对话框

package com.bw.day01;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Dialog extends android.app.Dialog {

    private TextView title;
    private TextView xinxi;
    private Button no;
    private Button yes;
    private String noStr;
    private String yesStr;
    private String titleStr;
    private String messageStr;

    public interface Noclicklistenner{
        void onclick();
    }
    private Noclicklistenner noclicklistenner;

    public void setNoclicklistenner(String str,Noclicklistenner noclicklistenner) {
        if (str!=null){
            noStr=str;
        }
        this.noclicklistenner = noclicklistenner;
    }

    public interface Yesclicklistenner{
        void onclick();
    }
    private Yesclicklistenner yesclicklistenner;

    public void setYesclicklistenner(String str,Yesclicklistenner yesclicklistenner) {
        if(str!=null){
            yesStr=str;
        }
        this.yesclicklistenner = yesclicklistenner;
    }

    public Dialog(@NonNull Context context) {
        super(context);
    }

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        title = (TextView) findViewById(R.id.title);
        xinxi = (TextView) findViewById(R.id.xinxi);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);

        if (messageStr!=null){
            xinxi.setText(messageStr);
        }
        if (titleStr!=null){
            title.setText(titleStr);
        }
        if (noStr!=null){
            no.setText(noStr);
        }
        if(yesStr!=null){
            yes.setText(yesStr);
        }
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noclicklistenner!=null){
                    noclicklistenner.onclick();
                }
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesclicklistenner.onclick();
            }
        });
    }
}

实现类

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Dialog dialog = new Dialog(this);
       dialog.setMessageStr("wucoia");
       dialog.setTitleStr("w无名之辈");
       dialog.setNoclicklistenner("nononono",new Dialog.Noclicklistenner() {
           @Override
           public void onclick() {

           }
       });
       dialog.setYesclicklistenner("yesyes",new Dialog.Yesclicklistenner() {
           @Override
           public void onclick() {

           }
       });
        dialog.show();
    }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!