【问题标题】:Show custom dialog when spinner is being clicked单击微调器时显示自定义对话框
【发布时间】:2023-03-20 10:52:01
【问题描述】:

目前,我的微调器处于dialog 模式。

        <Spinner
            android:spinnerMode="dialog"
            android:id="@+id/genderSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:entries="@array/gender_array"
            android:prompt="@string/gender_prompt" />

它会显示类似这样的东西

但是,有时,我希望能够控制要显示的对话框类型。例如,我想在单击微调器时显示一个带有日期选择器的对话框。

请注意,Google 日历确实有这样的控件。

我可以知道我怎样才能做到这一点吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    不要使用 Spinner,而是使用 TextView,并为其安装点击监听器。

    为了让TextView看起来像Spinner,这里是技巧

            <TextView
                android:id="@+id/date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.72"
                android:text="Aug 20, 2012"
                style="@android:style/Widget.Holo.Light.Spinner" />
    

            <TextView
                android:id="@+id/date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.72"
                android:text="Aug 20, 2012"
                style="@android:style/Widget.Holo.Spinner" />
    

    【讨论】:

    • 更好用style="@style/Widget.AppCompat.Spinner"
    【解决方案2】:

    我认为你在使用 Spinner 时会遇到麻烦。 Spinner 的概念是,它期望显示一个项目列表供用户选择。

    我认为您应该使用弹出包含 DatePicker 的自定义对话框的按钮,而不是 Spinner。如果您愿意,您甚至可以正确连接它以将所选日期显示为按钮上的文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多