简体   繁体   English

带有日期选择器的Android微调器,如Google Calendar应用

[英]Android spinner with date picker, like Google Calendar app

I'm trying to get a text box that looks like a spinner to activate a date picker dialog. 我正在尝试使用一个看起来像微调器的文本框来激活日期选择器对话框。 This is done in both the Google Calendar app and the Contacts app (for birthdate) on ICS. 这可以在Google日历应用和ICS上的通讯录应用(生日)中完成。 Do I need to use a spinner, and if so how do I change it's input view to be a date picker? 我是否需要使用微调器,如果是这样,我如何将其输入视图更改为日期选择器? Or if not, how do I get a text view to have the little triangle that usually indicates a spinner? 或者如果没有,我如何获得文本视图以获得通常表示微调器的小三角形?

Twaddington's comment on his answer is actually the right approach. Twaddington对他的答案的评论实际上是正确的方法。 What you need is to create a text view and apply the style 您需要的是创建文本视图并应用样式

style="@android:style/Widget.DeviceDefault.Light.Spinner"

Then you can create a click listener on the text view and use it to open a DatePickerDialog. 然后,您可以在文本视图上创建单击侦听器,并使用它来打开DatePickerDialog。 That can be accomplished as shown here: https://stackoverflow.com/a/8127571/332738 (If you follow the example, remember to add a default constructor to DatePickerDialogFragment so that your app does not crash on rotate) 这可以完成如下所示: https//stackoverflow.com/a/8127571/332738 (如果您按照示例,请记住向DatePickerDialogFragment添加默认构造函数,以便您的应用程序在旋转时不会崩溃)

I don't know if you still need this. 我不知道你是否还需要这个。 But in the Contacts app, it is achieved with the following: 但在“联系人”应用中,可通过以下方式实现:

<Button
    ...
    style="?android:attr/spinnerStyle"
    ... />

This should work over all Android versions, as it is available since api level 1: http://developer.android.com/reference/android/R.attr.html#spinnerStyle 这应该适用于所有Android版本,因为它自api级别1以来可用: http//developer.android.com/reference/android/R.attr.html#spinnerStyle

I'm not sure if this is what you're asking, but you should be able to follow the Date Picker tutorial on the Android developer website. 我不确定这是否是您所要求的,但您应该能够关注Android开发者网站上的日期选择器教程

Also, the DatePicker and DatePickerDialog classes might be worth a look. 此外, DatePickerDatePickerDialog类可能值得一看。

I would prefer below theme for Spinner like google contacts. 我更喜欢以下主题为Spinner,如谷歌联系人。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:spinnerStyle">@style/AppTheme.Form.Spinner</item>
        <item name="android:spinnerItemStyle">@style/AppTheme.Form.Spinner.Item</item>
    </style>

    <!-- Spinner Styles -->
    <style name="AppTheme.Form.Spinner" parent="Widget.AppCompat.Spinner">
        <item name="android:paddingRight">0dp</item>
        <item name="android:paddingEnd">0dp</item>
    </style>
    <style name="AppTheme.Form.Spinner.Item" parent="Widget.AppCompat.EditText">
        <item name="android:clickable">false</item>
    </style>
</resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM