自定义Dialog 对话框

在这里插入图片描述

自定义xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#12B6FF"
        android:paddingBottom="100dp">
        <TextView
            android:id="@+id/tv_bt"
            android:layout_marginTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="评价一下吧!"
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="#ffffff"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_ts"
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:text="喜欢吗?给个五星好评,鼓励\n我们会做的更好"
            android:gravity="center"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/bt_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="我要吐槽"
            android:textColor="@drawable/textcolor" />
        <Button
            android:id="@+id/bt_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="五星好评"
            android:textColor="@drawable/textcolor"/>
    </LinearLayout>
</LinearLayout>

自定义Java代码:

public class DivDialog extends Dialog {
    private TextView tvBt;
    private TextView tvTs;
    private Button btNo;
    private Button btOk;

    private String btStr;
    private String tsStr;
    private String noStr;
    private String yesStr;
    private onYesListener onYesListener;
    private onNoListener onNoListener;

    public void setOnYesListener(DivDialog.onYesListener onYesListener) {
        this.onYesListener = onYesListener;
    }

    public void setOnNoListener(DivDialog.onNoListener onNoListener) {
        this.onNoListener = onNoListener;
    }

    public void setBtStr(String btStr) {
        this.btStr = btStr;
    }

    public void setTsStr(String tsStr) {
        this.tsStr = tsStr;
    }

    public void setNoStr(String noStr) {
        this.noStr = noStr;
    }

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

    public DivDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.div_layout);
        tvBt = (TextView) findViewById(R.id.tv_bt);
        tvTs = (TextView) findViewById(R.id.tv_ts);
        btNo = (Button) findViewById(R.id.bt_no);
        btOk = (Button) findViewById(R.id.bt_ok);
        if(btStr != null){
            tvBt.setText(btStr);
        }
        if(tsStr != null){
            tvTs.setText(btStr);
        }
        if(noStr != null){
            btNo.setText(noStr);
        }
        if(yesStr != null){
            btOk.setText(yesStr);
        }
        btNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNoListener.onNoClick();
                dismiss();
            }
        });
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onYesListener.onYesClick();
                dismiss();
            }
        });
    }
    interface onYesListener{
        public void onYesClick();
    };
    interface onNoListener{
        public void onNoClick();
    };

}

实现Java代码:

DivDialog divDialog = new DivDialog(MainActivity.this);
                divDialog.setOnNoListener(new DivDialog.onNoListener() {
                    @Override
                    public void onNoClick() {
                        Toast.makeText(MainActivity.this, "憋着", Toast.LENGTH_SHORT).show();
                    }
                });
                divDialog.setOnYesListener(new DivDialog.onYesListener() {
                    @Override
                    public void onYesClick() {
                        Toast.makeText(MainActivity.this, "谢谢你的好评,我们会继续努力", Toast.LENGTH_SHORT).show();
                    }
                });
                divDialog.show();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值