如何有更好看的DatePicker? [英] How to have a better looking DatePicker?

查看:381
本文介绍了如何有更好看的DatePicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发一个Android应用程序,需要在2.2设备上运行;我使用HoloEverywhereLib与该用户界面是pretty的好。 我需要使用一个datepicker;如果我使用的默认组件,它有一个非常难看的样子:

I have to develop an Android app that needs to be run on 2.2 devices; i'm using HoloEverywhereLib with which the UI is pretty good. I need to use a DatePicker; if i use the default component, it has a really ugly look:

我想有像新一:

有没有办法做到这一点?

Is there a way to do it?

推荐答案

得到冰淇淋三明治风格的日期选择器的最简单方法是使用的 ActionBarSherlock库。

The easiest way to get the ice cream sandwich styled date picker is by using the ActionBarSherlock library.

然后创建对话框类:

public class DatePickerFragment extends SherlockDialogFragment implements android.app.DatePickerDialog.OnDateSetListener {

    private OnFragmentClickListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement listeners!");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // Do something with the date chosen by the user
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, monthOfYear);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        mListener.onFragmentClick(DialogDemonstrationActivity.DATE_PICKER_ACTION, c);
    }
}

活动监听器类:

The activity listener class:

public interface OnFragmentClickListener {
    public void onFragmentClick(int action, Object object);
}

该对话框可以与你的活动下启动:

The dialog can be launched with following from your activity:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                // Create and show the dialog.
                DatePickerFragment newFragment = new DatePickerFragment();
                newFragment.show(ft, null);

您的活动则必须实现监听,你是启动和运行。

Your activity then have to implement the listener and you are up and running.

这篇关于如何有更好看的DatePicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆