无法在TimePickerDIalog中解析方法DateFormat.is24HourFormat

时间:2017-01-06 14:25:13

标签: android android-datepicker android-timepicker

我正在尝试使用日期选择器和扩展对话框片段的时间选择器。我在运行应用程序时能够获取日期选择器,但是当我添加时间选择器时,我收到错误无法解析方法DateFormat.is24HourFormat。

我正在使用import android.support.v4.app.DialogFragment;根据帖子

TimepickerFragment show() method cannot be resolved

但问题仍然存在

主要活动

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;



public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void datePicker(View view) {
    DatePickerFragment fragment = new DatePickerFragment();
    fragment.show(getSupportFragmentManager(), "date");
}

public void TimePicker(View view) {
    TimePickerFragment fragment = new TimePickerFragment();
    fragment.show(getSupportFragmentManager(), "time");
}


private void setDate(final GregorianCalendar calendar) {

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
        ((TextView) findViewById(R.id.pic_date)).setText(dateFormat.format(calendar.getTime()));
    }
}


@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    GregorianCalendar cal = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        cal = new GregorianCalendar(year, month, day);
    }
    setDate(cal);
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar cal = null;
    ((TextView) findViewById(R.id.start_time)).setText(String.valueOf(hourOfDay)+ String.valueOf(minute));
}


public static class DatePickerFragment extends DialogFragment {

    @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(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
    }

}

public static class TimePickerFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of DatePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

}

}

3 个答案:

答案 0 :(得分:7)

我遇到了同样的问题。我通过导入

解决了这个问题
import android.text.format.DateFormat;

而不是

import android.icu.text.DateFormat;

这就是我所拥有的,或者在你的情况下我会尝试替换它:

import java.text.DateFormat;

(见这里:https://stackoverflow.com/a/15100358/3615383

答案 1 :(得分:1)

导入

  

android.text.format.DateFormat;

  

android.text.format.DateFormat.is24HourFormat(getActivity())

答案 2 :(得分:0)

我遇到了问题:DateFormat.is24HourFormat

但是后来我可以通过从以下位置更改导入来使用它:import java.text.DateFormat;

import android.text.format.DateFormat;

所以我可以在片段中使用:

TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(),
            hour, minute, DateFormat.is24HourFormat(getActivity()));