18
我已经阅读了很多文章,但是我找不到适用于此案例的任何文章。从对话框片段返回字符串返回到活动
我有一个时间选择器对话框,我把整数值放在一个字符串中,我需要把这个字符串返回到主要活动。然后
此字符串的值将被用于设置一个按钮的文本。
如果有人可以帮助我这个这将是最赞赏。
谢谢
对话片段
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@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
String Time =Integer.toString(hourOfDay) + " : " + Integer.toString(minute);
}
}
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btn = (Button) findViewById(R.id.start_time_button);
Button.setText(Time);
}
谢谢您的回复,我唯一不确定的是活动中的班级。我为“FragmentActivity”投入了什么 – user2059311 2013-02-27 20:28:09
'FragmentActivity'只是你的'活动'。你也可以在那里实现它。我会更新答案。 – Kirk 2013-02-27 21:15:34
我认为它几乎工作,我有1行上的最后一个错误 – user2059311 2013-02-27 21:59:47