Simple timepicker for a fragment activity

后端 未结 2 611
礼貌的吻别
礼貌的吻别 2020-12-22 04:37

I am trying to launch a timepicker from a edittext in a fragment

Buffet_offerings_breakfast_menu2.java

p         


        
相关标签:
2条回答
  • 2020-12-22 05:09

    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());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 05:09

    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;
    }
    
    0 讨论(0)
提交回复
热议问题