如何在Fragment中设置时间选择器

时间:2015-08-12 11:45:14

标签: android

在我的应用中,我有两个editText,一个用于约会,另一个用于时间选择器。日期选择器工作正常,但时间选择器会抛出错误。

这是我的代码:

    EditText searchDate;
    EditText searchTime;
    DatePickerDialog searchDatePickerDialog;
    TimePickerDialog timePickerDialog;
    SimpleDateFormat timeFormatter;
    SimpleDateFormat dateFormatter;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.search, container,false);


    searchDate = (EditText)view.findViewById(R.id.search_date);
    searchDate.setInputType(InputType.TYPE_NULL);
    searchTime = (EditText)view.findViewById(R.id.search_time);
    searchTime.setInputType(InputType.TYPE_NULL);
  
  searchDate.setOnClickListener(this);
        searchTime.setOnClickListener(this);

        dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        searchDate.setText(dateFormatter.format(new Date()));

        timeFormatter = new SimpleDateFormat("HH:mm:ss",Locale.US);
        searchTime.setText(timeFormatter.format(new Date()));

        Calendar newCalendar = Calendar.getInstance();


        searchDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                searchDate.setText(dateFormatter.format(newDate.getTime()));
            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

        timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c = Calendar.getInstance();
                c.set(hourOfDay,minute);
                searchTime.setText(timeFormatter.format(c.getTime()));
            }
        },newCalendar.get(Calendar.HOUR_OF_DAY),newCalendar.get(Calendar.MINUTE));

        return view;
    }

    @Override
    public void onClick(View v) {
        if (v == searchDate) {
            searchDatePickerDialog.show();
        }
        else if (v == searchTime){
            timePickerDialog.show();
        }
    }

这就是我获得的错误:

  

无法解析构造函数'TimePickerDialog(   android.support.v4.app.FragmentActivity,匿名   android.app.TimePickerDialog.OnTimeSetListener,INT,INT)'

1 个答案:

答案 0 :(得分:0)

正如@Boss所说,你需要添加一个布尔值作为最后一个参数。发布代码以关闭问题。给他的信用。

timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                c.set(Calendar.MINUTE, minute);
                searchTime.setText(timeFormatter.format(c.getTime()));
            }
        },newCalendar.get(Calendar.HOUR_OF_DAY),newCalendar.get(Calendar.MINUTE), true);