简体   繁体   English

Android时间选择器

[英]Android time picker

I have used the code on this Android developers page to create a time picker when a button is pressed. 按下按钮时,我已使用此Android开发人员页面上的代码创建时间选择器。 However, I'm not getting the right look for it. 但是,我没有找到正确的外观。 I'm targeting my app to API 18 and the minimum is 10, because I want to support 2.3 due to the high usage . 我将我的应用定位为API 18,最小值为10,因为由于使用率高 ,我想支持2.3。 The minimum required SDK doesn't seem to have effect on the problem (I set it to 16): 所需的最低SDK似乎对问题没有影响(我将其设置为16):

I'm debugging the application on my HTC One S with Android 4.1.2, API 16. I get this kind of time picker but I want it to look modern, like in the Android developers page linked earlier. 我正在使用Android 4.1.2,API 16在HTC One S上调试应用程序。我得到了这种时间选择器,但是我希望它看起来很现代,就像前面链接的Android开发人员页面中一样。

http://www.b2creativedesigns.com/Tutorials/TimePicker/tutorial_timepicker2.png http://www.b2creativedesigns.com/Tutorials/TimePicker/tutorial_timepicker2.png

Why does it use this prehistoric looking time picker? 为什么使用这种史前时间选择器?

I create the dialog using the following snippet. 我使用以下代码段创建对话框。 I call it from a FragmentActivity. 我从FragmentActivity调用它。

DialogFragment newFragment = TimePickerFragment.newInstance(2);
newFragment.show(getSupportFragmentManager(), "endtime");

This is the code for TimePickerFragment . 这是TimePickerFragment的代码。

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
    private static int id;

    public static TimePickerFragment newInstance(int _id) {
        Bundle args = new Bundle();
        args.putInt("id", _id);

        id = _id;

        TimePickerFragment f = new TimePickerFragment();
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        id = getArguments().getInt("id");

        int hour = start / 60;  // start is a global variable, no problems in these
        int minute = start % 60;

        if(id == 2) {
            hour = end / 60;
            minute = end % 60;
        }

        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        System.out.println(hourOfDay + ":" + minute);
        setTime(id, hourOfDay, minute);
    }

}

You need to use the Holo theme for the Holo widgets to work in your app. 您需要对Holo小部件使用Holo主题才能在您的应用中使用。 For example : 例如 :

in your Manifest, under application : 在清单中,在应用程序下:

android:theme="@style/AppTheme"

and in your res/values folder : 并在您的res / values文件夹中:

<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
</style>

while using this in values-v11 and values-v14 : 在values-v11和values-v14中使用时:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar">
</style>

The system will switch between Theme.Light and Theme.Holo.Light depending on the Android version on the device. 系统将根据设备上的Android版本在Theme.Light和Theme.Holo.Light之间切换。

Use ContextThemeWrapper to explicitly provide your Holo-based theme for the Dialog , eg 使用ContextThemeWrapperDialog显式提供基于Holo的主题,例如

return new TimePickerDialog(
        new ContextThemeWrapper(getActivity(), R.style.YourAppStyleInheritingFromHolo),
        this, hour, minute, DateFormat.is24HourFormat(getActivity()));

You might want to take a look at this library. 您可能想看一下这个库。 It is a backported TimePicker from Android 4.2 which works from android 2.1 upwards. 它是从Android 4.2向后移植的TimePicker,可从android 2.1向上运行。

https://github.com/SimonVT/android-timepicker https://github.com/SimonVT/android-timepicker

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

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