我正在使用DatePickerDialog提示用户输入日期。它在模拟器和我的Samsung Galaxy Nexus上都可以正常工作,但是在我的Samsung Galaxy Tab 2上没有调用onDateSet。

我注意到,该对话框更大,除正常旋转视图外还显示一个日历。这可能是问题吗?

这是一些代码:

import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import com.cbit.gtbetapp.R;
import com.cbit.gtbetapp.gui.racedata.MeetingDataActivity;
import com.cbit.gtbetapp.gui.racedata.MeetingListFragment;
import com.cbit.gtbetapp.logic.Utility;

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    protected Date date = null;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current date as the default date in the picker
        final Calendar c = Utility.getToday();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        date = c.getTime();

        // Create a new instance of DatePickerDialog and return it
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day) {
            @Override
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                super.onDateChanged(view, year, month, day);
                setTitle(getString(R.string.date_picker_title));
            }
        };
        dialog.setTitle(getString(R.string.date_picker_title));

        dialog.setButton(DatePickerDialog.BUTTON_POSITIVE,
                getString(android.R.string.ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                        Intent intent = new Intent(getActivity(), MeetingDataActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra(MeetingListFragment.EXTRA_DATE, date.getTime());
                        startActivity(intent);
                    }
                });
        dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE,
                getString(R.string.button_cancel),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        date = c.getTime();
    }
}


是什么原因造成的?平板电脑有毛病?我想念什么吗?谁能想到解决方法?

非常感谢!

最佳答案

令我惊讶的是它根本没有用。当执行默认的onDateSet()行为时,会调用onClick,但是由于完全设置了新按钮,因此摆脱了原来的行为。您可以check the code out。为什么不在onDateSet()中开始活动?如果要设置文本,则可以获取每个按钮而不是设置它们(getButton(BUTTON_POSITIVE))。

另外,请注意Jelly Bean中存在一个新错误,其中onDateSet将被调用两次,即使您按回去,它们也会被调用,并且它们还删除了默认的“取消”按钮。去搞清楚。

10-08 07:50