在android中使用一个简单的对话框片段

时间:2013-11-18 05:23:32

标签: android timepicker dialogfragment

我试图点击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);
    }
}

我收到两个错误 ::

ERROR1 ::

for line

 newFragment.show(getSupportFragmentManager(), "timePicker");

作为 -

The method getSupportFragmentManager() is undefined for the type new View.OnClickListener(){}

误差2 ::

for line

return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));

作为 -

TimePickerDialog cannot be resolved to a type

我该如何解决这个问题呢?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在Fragment类中使用getFragmentmanager()而不是getSupportFragmentManager()。 在您的FragmentActivity中实现TimePickedListener接口并更新片段

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);
    }

}

这将是你的片段。

@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)); 
}

答案 2 :(得分:0)

我认为您的问题来自getSupportFragmentManager() newFragment.show(getSupportFragmentManager(), "timePicker");。您的TimePickerDialogFragment,它被提交到Buffet_offerings_breakfast_menu2片段(嵌套片段)。因此,请使用getChildFragmentManager()代替getSupportFragmentManager()