AlertDialog的简单调用和自定义

原生的简单调用

代码如下:

AlertDialog dialog;
dialog=new AlertDialog.Builder(context)
                .setTitle("This is a title!")
                .setMessage("This is a message!")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyUtils.showToast(context,"Click Ok!Then do something.");
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

自定义AlertDialog

java代码:

private void initCustomDialog(int resId) {//传入自定义xml布局资源id
    View customView=LayoutInflater.from(context).inflate(resId,null);
    dialog=new AlertDialog.Builder(context)
            .setView(customView)
            .create();
    TextView tvCancel=customView.findViewById(R.id.tvCancel);
    TextView tvSure=customView.findViewById(R.id.tvSure);
    tvCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    tvSure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MyUtils.showToast(context,"do something!");
            dialog.dismiss();
        }
    });
    dialog.show();
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <TextView
        android:text="CustomTitle"
        android:textSize="25dp"
        android:gravity="center"
        android:background="#ff00ff"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:gravity="center"
        android:textSize="20dp"
        android:background="#55ff55"
        android:text="This is a Custom Message!"
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#88ffff"
        android:layout_height="50dp">
        <TextView
            android:id="@+id/tvCancel"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Cancel"
            android:textSize="20dp"
            android:gravity="center"
            android:layout_height="match_parent" />
        <View
            android:background="#55ff55"
            android:layout_width="1dp"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/tvSure"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Ok"
            android:textSize="20dp"
            android:gravity="center"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

运行效果图如下:
这里写图片描述
GitHub地址:https://github.com/BingaChen/project_pool
欢迎关注我的公众号,和我一起每天进步一点点!
欢迎关注我的公众号,和我一起每天进步一点点!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值