对话框之自定义对话框

思路:点击一个按钮,弹出一个自定义对话框
1.首先在主布局定义一个按钮button,为button添加圆角属性,设置点击事件
2.定义一个自定义布局,用来设置对话框的样式
3.写单击事件,加载实例化布局

加载布局的3中方式
实例化布局(参数:要填充的布局,父组件(是不是要把这个布局填充到父组件中,通常用null)
View v1 = LayoutInflater.from(this).inflate(R.layout.login,null);
View v2= getLayoutInflater().inflate(R.layout.login,null);
builder.setView(R.layout.login);
代码如下

<Button
        android:background="@drawable/button_shape"
        android:id="@+id/button_register"
        android:layout_below="@id/editText_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义对话框"
        android:onClick="customClick"
        android:textSize="20sp" />

定义shape

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <corners android:radius="10dp"/>
        <solid android:color="@color/colorPrimary"/>
        <stroke android:width="10dp" android:color="@color/colorAccent"/>
</shape>

定义自定义布局

<EditText
        android:id="@+id/editText_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:textSize="20sp"
        />
    <EditText
        android:id="@+id/editText_pass"
        android:layout_below="@id/editText_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/button_login"
        android:layout_below="@id/editText_pass"
        android:text="登录"
        android:textSize="20sp"
        android:layout_marginLeft="80dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button_register"
        android:layout_below="@id/editText_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="20sp"
        android:layout_marginRight="80dp"
        android:layout_alignParentRight="true"/>

设置单击事件

public void customClick(View view){
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("选择登录");
        builder.setCancelable(false);
        View v = getLayoutInflater().inflate(R.layout.login,null);
        builder.setView(v);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消登录", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        builder.show();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值