如何有更好看的DatePicker?好看、DatePicker

由网友(久浪不归)分享简介:我要开发一个Android应用程序,需要在2.2设备上运行;我使用HoloEverywhereLib与该用户界面是pretty的好。我需要使用一个datepicker;如果我使用的默认组件,它有一个非常难看的样子:I have to develop an Android app that needs to be r...

我要开发一个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.

阅读全文

相关推荐

最新文章