<legend id='cwqAe'><style id='cwqAe'><dir id='cwqAe'><q id='cwqAe'></q></dir></style></legend>

<tfoot id='cwqAe'></tfoot>
  • <i id='cwqAe'><tr id='cwqAe'><dt id='cwqAe'><q id='cwqAe'><span id='cwqAe'><b id='cwqAe'><form id='cwqAe'><ins id='cwqAe'></ins><ul id='cwqAe'></ul><sub id='cwqAe'></sub></form><legend id='cwqAe'></legend><bdo id='cwqAe'><pre id='cwqAe'><center id='cwqAe'></center></pre></bdo></b><th id='cwqAe'></th></span></q></dt></tr></i><div id='cwqAe'><tfoot id='cwqAe'></tfoot><dl id='cwqAe'><fieldset id='cwqAe'></fieldset></dl></div>
  • <small id='cwqAe'></small><noframes id='cwqAe'>

        <bdo id='cwqAe'></bdo><ul id='cwqAe'></ul>

      1. 将字符串从对话框片段返回到活动

        时间:2023-08-31

        • <bdo id='Df0LC'></bdo><ul id='Df0LC'></ul>

                <tbody id='Df0LC'></tbody>
              <legend id='Df0LC'><style id='Df0LC'><dir id='Df0LC'><q id='Df0LC'></q></dir></style></legend>
              <tfoot id='Df0LC'></tfoot>

              <small id='Df0LC'></small><noframes id='Df0LC'>

                1. <i id='Df0LC'><tr id='Df0LC'><dt id='Df0LC'><q id='Df0LC'><span id='Df0LC'><b id='Df0LC'><form id='Df0LC'><ins id='Df0LC'></ins><ul id='Df0LC'></ul><sub id='Df0LC'></sub></form><legend id='Df0LC'></legend><bdo id='Df0LC'><pre id='Df0LC'><center id='Df0LC'></center></pre></bdo></b><th id='Df0LC'></th></span></q></dt></tr></i><div id='Df0LC'><tfoot id='Df0LC'></tfoot><dl id='Df0LC'><fieldset id='Df0LC'></fieldset></dl></div>

                  本文介绍了将字符串从对话框片段返回到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我已经阅读了很多关于此的帖子,但我找不到任何适用于此案例的帖子.

                  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.

                  谢谢

                  对话框片段

                  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);
                  
                  
                      }
                  

                  推荐答案

                  首选方法是使用回调从 Fragment 获取信号.另外,这也是Android在与Activity通信

                  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

                  对于您的示例,在您的 DialogFragment 中,添加一个接口并注册它.

                  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");
                      }
                  }
                  

                  现在在你的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
                      }
                  }
                  

                  现在在您的 DialogFragment 中,当用户单击 OK 按钮时,通过您的回调将该值发送回 Activity.

                  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);
                  }
                  

                  这篇关于将字符串从对话框片段返回到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:AlertDialog 不显示正负按钮 下一篇:AppCompatActivity 作为没有标题的对话框

                  相关文章

                  最新文章

                  <legend id='RH5Um'><style id='RH5Um'><dir id='RH5Um'><q id='RH5Um'></q></dir></style></legend>
                  • <bdo id='RH5Um'></bdo><ul id='RH5Um'></ul>
                  1. <tfoot id='RH5Um'></tfoot>
                  2. <i id='RH5Um'><tr id='RH5Um'><dt id='RH5Um'><q id='RH5Um'><span id='RH5Um'><b id='RH5Um'><form id='RH5Um'><ins id='RH5Um'></ins><ul id='RH5Um'></ul><sub id='RH5Um'></sub></form><legend id='RH5Um'></legend><bdo id='RH5Um'><pre id='RH5Um'><center id='RH5Um'></center></pre></bdo></b><th id='RH5Um'></th></span></q></dt></tr></i><div id='RH5Um'><tfoot id='RH5Um'></tfoot><dl id='RH5Um'><fieldset id='RH5Um'></fieldset></dl></div>

                    <small id='RH5Um'></small><noframes id='RH5Um'>