[android](学习笔记6)为应用程序添加对话框(2)

如何为对话添加视图?

上一篇文章中只能显示一个空的对话框,除了标题和一个确定按钮,别的什么都没有,这样的对话框没有任何意义。所以本文需要解答如何为对话框添加内容的问题。

我们的对话框是通过AlertDialog.Builder创建的,在创建之前,可以跟我们几个机会去配置对话框。为了给对话框添加视图,可以通过调用AlertDialog.Builder的setView方法实现。不过,首先,你得有一个视图对象,可以通过xml布局文件的形式生成一个视图对象,直接看代码(包括下面的布局文件):

public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = getActivity().getLayoutInflater().inflate(
                R.layout.dialog_date, null);
        // 要在屏幕上显示DialogFragment时,托管activity的FragmentManager会调用该方法创建对话框实例
        /*
         * 采用“流接口”的方式创建AlertDialog实例:首先通过传入上下文对象给AlertDialog.Builder类的构造方法
         * 创建一个AlertDialog.Builder实例,开头的new就是起到了这个作用;创建出的AlertDialog.Builder实例紧
         * 接着调用两个AlertDialog.Builder类的set方法配置对话框(如需要为PositiveButton添加响应代码,需实现
         * DialogInterface
         * .OnClickListener,并替换null);最后调用AlertDialog.Builder类的create方法创建AlertDialog
         * 实例。
         */
        return new AlertDialog.Builder(getActivity()).setView(v)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(android.R.string.ok, null).create();
}
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:calendarViewShown="false" >
    

</DatePicker>

 

 

转载于:https://www.cnblogs.com/leo-duduchen/p/4489553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值