手机卫士学习09-自定义对话框

本文介绍如何在Android应用中创建带有输入框的自定义AlertDialog。通过设置布局文件,将包含文本提示和按钮的视图添加到对话框中,实现更加灵活的交互界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android系统的AlertDialog.Builder    只有标题,信息,按钮。
如果我们想制作一个带输入框的对话框,需要我们自定义一个对话框。

1    设置我们对话框的布局文件
2    将布局文件设置到AlertDialog中
3    显示我们的对话框


----------------------------------------------------------

1    设置我们对话框的布局文件

效果图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dip"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:background="#66ff6600"
        android:gravity="center"
        android:text="设置密码"
        android:textSize="30sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:hint="请再次输入密码"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:text="确定" />

        <Button
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>


2    将布局文件设置到AlertDialog中
3    显示我们的对话框

	/**
	 * 设置密码对话框
	 */
	private void showSetupPwdDialog() {
		AlertDialog.Builder builder =new Builder(HomeActivity.this);
		//自定义一个布局文件
		View view =View.inflate(HomeActivity.this, R.layout.dialog_setup_pwd, null);
		builder.setView(view);
		builder.show();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值