0

我正在尝试在单击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

我该如何解决这个问题!

4

3 回答 3

0

I think your problem is from getSupportFragmentManager() at newFragment.show(getSupportFragmentManager(), "timePicker");. Your TimePickerDialog is a Fragmentwhich is committed into Buffet_offerings_breakfast_menu2 fragment (nested fragment). So use getChildFragmentManager() instead of getSupportFragmentManager().

于 2014-04-04T06:21:06.597 回答
0

Use getFragmentmanager() instead of getSupportFragmentManager() in your Fragment class. Implement TimePickedListener interface in ur FragmentActivity and update the fragment

public class MainActivity extends FragmentActivity implements TimePickedListener {

    Calendar time =null;
    Fragment_Main fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onTimePicked(Calendar time){
        fragment = (Fragment_Main) getSupportFragmentManager().findFragmentById(R.id.new_homescreen_two_fragment);
        fragment.onTimePicked(time);
    }

}

And this will be in ur fragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    view = inflater.inflate(R.layout.buffet_offerings_breakfast_menu2, container, false);
    from = (EditText) view.findViewById(R.id.etDepDate);

    from.setText("Here");

    from.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // show the time picker dialog
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker");
        }
    }); // this is on onCreate() method..
    return view;
}

public void onTimePicked(Calendar time) {
    // display the selected time in the TextView
    from.setText(DateFormat.format("h:mm a", time)); 
}
于 2013-11-18T10:10:05.633 回答
0

对于第一个错误,您可以将 getSupprotFragmentManager() 存储在类中声明的私有变量中,然后在此处使用它。

于 2013-11-18T05:56:45.283 回答