timepicker
但是当我点击设置时
timepicker
我看不到edittext
上的值,它是空的timepicker
pops值不是edittext
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());
}
}
}
}
谢谢!