Android spinner with date picker, like Google Calendar app

后端 未结 4 2023
一生所求
一生所求 2020-12-04 13:28

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) o

相关标签:
4条回答
  • 2020-12-04 13:50

    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.

    Also, the DatePicker and DatePickerDialog classes might be worth a look.

    0 讨论(0)
  • 2020-12-04 13:53

    I would prefer below theme for Spinner like google contacts.

    <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>
    
    0 讨论(0)
  • 2020-12-04 13:54

    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

    0 讨论(0)
  • 2020-12-04 14:00

    Twaddington's comment on his answer is actually the right approach. 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. 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)

    0 讨论(0)
提交回复
热议问题