片段的简单Timepicker没有将值设置为edittext

时间:2013-12-28 10:27:20

标签: android android-fragments

I am referring to my previous question that involves launching a timepicker from fragment

  • 我在项目中成功实施但
  • 我可以弹出timepicker但是当我点击设置时 timepicker我看不到edittext上的值,它是空的
  • 所以即使timepicker pops值不是edittext
  • 中的st
  • 我在哪里做错了

MainActivity.java

public class MainActivity extends FragmentActivity implements PickTime{

    Fragment1 newFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newFragment=new Fragment1();
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.add(newFragment, "Fragment-Tag");
        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);
        }
    }


}

Fragment1.java

public class Fragment1 extends Fragment {

    EditText ed;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.fragment1, container,false);

        return view;
    }


    @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 arg0) {
                // TODO Auto-generated method stub
            TimerPickerFragment newFragment = new TimerPickerFragment();
            newFragment.show(getFragmentManager(), "tag");

            }
        });

    }

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

    }



} 

TimerPickerFragment.java

public class TimerPickerFragment 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 个答案:

没有答案