返回从对话片段字符串返回到活动 [英] Returning String from Dialog Fragment back to Activity
问题描述
我看过这么多的职位,但我找不到任何适用于这种情况。
我有一个时间选择器对话框,我已经把整数值一起在一个字符串,我需要得到这个字符串返回到的主要活动。
这个字符串值将被用于设置按钮上的文字。
如果有人可以帮助我这个这将是最AP preciated。
感谢您
对话片段
公共类TimePickerFragment扩展DialogFragment实现TimePickerDialog.OnTimeSetListener {
@覆盖
公共对话onCreateDialog(包savedInstanceState){
//使用当前时间作为选择器的默认值
最后的日历C = Calendar.getInstance();
INT小时= c.get(Calendar.HOUR_OF_DAY);
INT分钟= c.get(Calendar.MINUTE);
//创建TimePickerDialog的新实例,并将其返回
返回新TimePickerDialog(getActivity(),这一点,小时,分钟,DateFormat.is24HourFormat(getActivity()));
}
公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
//做一些与用户所选择的时间
字符串时间= Integer.toString(hourOfDay)+:+ Integer.toString(分钟);
}
}
code
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_second);
按钮BTN =(按钮)findViewById(R.id.start_time_button);
Button.setText(时间);
}
在preferred方法是使用一个回调函数来获得从片段
的信号。此外,这是建议的Android推荐的方法在与活动沟通
有关你的榜样,在你的 DialogFragment
,添加一个接口和注册。
公共静态接口OnCompleteListener {
公共抽象无效的onComplete(字符串时);
}
私人OnCompleteListener mListener;
//确保活动实现了它
公共无效onAttach(活动活动){
super.onAttach(活动)
尝试 {
this.mListener =(OnCompleteListener)的活动;
}
赶上(最终ClassCastException异常E){
抛出新ClassCastException异常(activity.toString()+必须实现OnCompleteListener);
}
}
现在在你的活动实现这个的接口的
公共类MyActivity扩展活动实现MyDialogFragment.OnCompleteListener {
// ...
公共无效的onComplete(字符串时){
//对话片段完成后,它会调用这个回调。
//这里使用字符串
}
}
现在在你的 DialogFragment
,当用户单击OK按钮,发送值回活动
通过回调。
公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
字符串时间= Integer.toString(hourOfDay)+:+ Integer.toString(分钟);
this.mListener.onComplete(时间);
}
I have read many posts on this but I can't find any that apply to this case.
I have a time picker dialog and I have put the integer values together in a string and I need to get this string back to the main activity.
This string value will then be used to set the text of a button.
If someone could help me with this it would be most appreciated.
Thank you
Dialog Fragment
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);
}
}
Code
@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);
}
The preferred method is to use a callback to get a signal from a Fragment
. Also, this is the recommended method proposed by Android at Communicating with the Activity
For your example, in your DialogFragment
, add an interface and register it.
public static interface OnCompleteListener {
public abstract void onComplete(String time);
}
private OnCompleteListener mListener;
// make sure the Activity implemented it
public void onAttach(Activity activity) {
super.onAttach(activity)
try {
this.mListener = (OnCompleteListener)activity;
}
catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}
Now implement this interface in your Activity
public class MyActivity extends Activity implements MyDialogFragment.OnCompleteListener {
//...
public void onComplete(String time) {
// After the dialog fragment completes, it calls this callback.
// use the string here
}
}
Now in your DialogFragment
, when a user clicks the OK button, send that value back to the Activity
via your callback.
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String time = Integer.toString(hourOfDay) + " : " + Integer.toString(minute);
this.mListener.onComplete(time);
}
这篇关于返回从对话片段字符串返回到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!