2017-04-15 121 views
1

我想在Android中實現一個簡單的TimePickerDialog,但我收到錯誤。我下面從谷歌這個教程:https://developer.android.com/guide/topics/ui/controls/pickers.htmlAndroid中的TimePickerDialog

在我的代碼,我有一個名爲setFromTimeButton被點擊但是應該顯示與TimePicker視圖DialogFragment的時候,我有一個編譯錯誤按鈕:

Cannot resolve symbol TimePickerFragment() 

你知道爲什麼嗎?謝謝!

這是我的課:

package it.bitrack.fabio.bitrack; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
//import android.app.Fragment; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import java.util.List; 

import android.app.Dialog; 
import android.text.format.DateFormat; 

import android.app.TimePickerDialog; 
import android.app.DatePickerDialog; 

import android.icu.util.Calendar; 
import android.widget.DatePicker; 
import android.widget.TimePicker; 

import android.widget.Button; 
import android.widget.TextView; 


/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link ScheduleView.OnFragmentInteractionListener} interface 
* to handle interaction events. 
* Use the {@link ScheduleView#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class ScheduleView extends DialogFragment implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener { 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    Button setFromTimeButton; 

    android.support.v4.app.FragmentManager fragmentManager; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public ScheduleView() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment ScheduleView. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static ScheduleView newInstance(String param1, String param2) { 
     ScheduleView fragment = new ScheduleView(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_schedule_view, container, false); 

     setFromTimeButton = (Button) view.findViewById(R.id.setFromTimeButton); 

     fragmentManager = getFragmentManager(); 

     setFromTimeButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       DialogFragment newFragment = new TimePickerFragment(); 
       newFragment.show(fragmentManager, "timePicker"); 
      } 
     }); 


     return view; 
    } 


    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { 

    } 

    @Override 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for 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 TimePickerDialog and return it 
     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 
} 
+1

您問題中的代碼沒有'TimePickerFragment'。創建一個在[鏈接到的文檔]中(https://developer.android.com/guide/topics/ui/controls/pickers.html)。 – CommonsWare

+0

@CommonsWare,所以我需要創建一個名爲TimePickerFragment的不同類? –

+0

是 - 請參閱rafsanahmad007的答案。 – CommonsWare

回答

2

您需要添加下面的類...

public static class TimePickerFragment extends DialogFragment 
          implements TimePickerDialog.OnTimeSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for 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 TimePickerDialog and return it 
     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // Do something with the time chosen by the user 
    } 
} 

現在你可以使用稱之爲:

DialogFragment newFragment = new TimePickerFragment(); 
newFragment.show(fragmentManager, "timePicker"); 
+0

是否有必要使用DialogFragment,而不是直接在Fabio的答案中使用TimePickerDialog?我有些得到 爲什麼應該使用DialogFragment(https://developer.android.com/guide/topics/ui/dialogs.html)。但是,它看起來好像你必須創建一個單獨的java文件來實現這個(我寧願不這樣做)。 –

+1

你可以在同一個類中編寫代碼而不需要創建java文件..我猜你也可以使用timepicker對話框,如果它適合你的需要的話。 – rafsanahmad007

1

我可以做我的代碼通過使用下面的代碼工作:

setFromTimeButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      final Calendar c = Calendar.getInstance(); 
      int hour = c.get(Calendar.HOUR_OF_DAY); 
      int minute = c.get(Calendar.MINUTE); 

      TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

       } 
      }, hour, minute, false); 

      timePickerDialog.show(); 

     } 
    }); 
+0

簡單得多,謝謝! –

相關問題