从对话框片段返回字符串返回到活动 [英] Returning String from Dialog Fragment back to Activity
问题描述
我已经阅读了很多帖子,但我找不到适用于这种情况的任何内容。
我有一个时间选择器对话框,我把整数值一起在一个字符串中,我需要将此字符串返回到主要活动。
然后,此字符串值将用于设置按钮的文本。 p>
如果有人可以帮助我,这将是非常感谢。
谢谢
对话框片段
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//使用当前时间作为选择器的默认值
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//创建一个新的TimePickerDialog实例并返回它
返回新的TimePickerDialog(getActivity(),this,hour,minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker视图,int hourOfDay,int minute){
//按照用户选择的时间执行某些操作
String Time = Integer .toString(hourOfDay)+:+ Integer.toString(minute);
}
}
代码
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
按钮btn =(Button)findViewById(R.id.start_time_button);
Button.setText(Time);
}
p>首选方法是使用回调从 Fragment
获取信号。此外,这是Android在与活动通信中推荐的方法
对于您的示例,在您的 DialogFragment
中添加一个界面并注册。
public $静态接口OnCompleteListener {
public abstract void onComplete(String time);
}
private OnCompleteListener mListener;
//确保Activity实现了
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
尝试{
this.mListener =(OnCompleteListener)活动;
}
catch(final ClassCastException e){
throw new ClassCastException(activity.toString()+必须实现OnCompleteListener);
}
}
现在实现这个界面在您的活动
public class MyActivity extends Activity implements MyDialogFragment.OnCompleteListener {
// ...
public void onComplete(String time){
//对话片段完成后,它调用此回调。
//使用字符串
}
}
现在在您的 DialogFragment
中,当用户单击确定按钮时,通过您的回调将该值发送回活动
。 / p>
public void onTimeSet(TimePicker view,int hourOfDay,int minute){
String time = Integer.toString(hourOfDay) +:+ Integer.toString(minute);
this.mListener.onComplete(time);
}
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
@Override
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屋!