我正在尝试在单击edittext时启动 timePicker
我已经构建了大部分东西!
Buffet_offerings_breakfast_menu2.java
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.androidbegin.jsonparsetutorial.R;
public class Buffet_offerings_breakfast_menu2 extends Fragment{
RadioGroup radioGroup;
EditText from;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
from=(EditText) getActivity().findViewById(R.id.from_lunch_edit_text_id);
from.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// show the time picker dialog
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
}); // this is on onCreate() method..
}
public void onTimePicked(Calendar time)
{
// display the selected time in the TextView
from.setText(DateFormat.format("h:mm a", time));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
final View view=inflater.inflate(R.layout.buffet_offerings_breakfast_menu2, container, false);
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if(checkedId == R.id.SelectDaysRG_ID)
{
view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.VISIBLE);
}
else if(checkedId == R.id.WeekdaysRG_ID)
{
view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.INVISIBLE);
}
else if(checkedId == R.id.WeekendsRG_ID)
{
view.findViewById(R.id.linearLayout_individualdays).setVisibility(View.INVISIBLE);
}
}
});
return view;
}
}
TimePickerFragment.java
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.TimePicker;
public class TimePickerFragment extends DialogFragment implements OnTimeSetListener
{
private TimePickedListener mListener;
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()));
}
@Override
public void onAttach(Activity activity)
{
// when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
super.onAttach(activity);
try
{
mListener = (TimePickedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
}
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
// when the time is selected, send it to the activity via its callback interface method
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
mListener.onTimePicked(c);
}
public static interface TimePickedListener
{
public void onTimePicked(Calendar time);
}
}
我收到两个错误::
错误1 ::
用于线
newFragment.show(getSupportFragmentManager(), "timePicker");
作为——
The method getSupportFragmentManager() is undefined for the type new View.OnClickListener(){}
错误2 ::
用于线
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
作为——
TimePickerDialog cannot be resolved to a type
我该如何解决这个问题!