Simple timepicker for a fragment activity

一笑奈何 提交于 2019-11-29 16:28:53

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

http://developer.android.com/training/basics/fragments/communicating.html

You can interface as a call back to the activity. So get the time in activity and then communicate to fragment.

Example:

For the example sake i am extending Activity. You can extend FragmentAcitivty ans use support library and make proper imports.

    public class MainActivity extends Activity implements PickTime{

        EditFragment newFragment; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            newFragment = new EditFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.container, newFragment);
            //transaction.addToBackStack(null);
            transaction.commit();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public void returnTime(String value) {
            // TODO Auto-generated method stub
            if(newFragment.isVisible())
            {
                newFragment.setEdittextvalue(value);
            }

        }

    }

Fragment with just a Edittext

public class EditFragment extends Fragment {

    EditText ed;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragmentlayout,container,false);

        return v;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        ed = (EditText) getView().findViewById(R.id.editText1);
        ed.setOnClickListener(new  OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              TimePickerFragment newFragment = new TimePickerFragment();
              newFragment.show(getFragmentManager(), "timePicker");
            }

        });
    }
    public void setEdittextvalue(String value) {
        // TODO Auto-generated method stub
        ed.setText(value);

    }
}

TimerPickerFragment

public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {


    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mCallback = (PickTime) activity;
    }

    public interface PickTime
    {
        public void returnTime(String value);

    }

    PickTime mCallback;
    @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

        if(mCallback!=null)
        {
            StringBuilder sb = new StringBuilder();
            sb.append(hourOfDay);
            sb.append(":");
            sb.append(minute);
            mCallback.returnTime(sb.toString());
        }
    }
}

initialize mCallback in Buffet_offerings_breakfast_menu2 like this

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    mCallback = (PickTime) activity;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!