3

当我按下活动上的按钮时,会调用我的 DialogFragment 类。我希望通过此 DialogFragment 设置的日期显示在按钮文本上。FindViewById 拒绝被类识别。

我发现了一个类似的问题,但我无法将其与我的案例联系起来。

代码:

  1. 调用对话片段的按钮:

    public void datepicker(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker"); 
    }
    
  2. 对话片段代码:

    public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceSateate) {
    
            final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
    
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }
    
            public void onDateSet(DatePicker view, int year, int month, int day) {
                // Do something with the date chosen
            }
        }
    }
    
4

4 回答 4

10

在 DialogFragment 中负责在用户设置日期时收到通知的方法中,执行此操作

Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);
于 2012-12-24T19:23:27.040 回答
3

为 DatePicker 片段创建类:

    public class SelectDateFragment extends DialogFragment implements
                                     DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

            Calendar now = Calendar.getInstance();
            year = now.get(Calendar.YEAR);
            month = now.get(Calendar.MONTH);
            day = now.get(Calendar.DATE);

         return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        orderDateEditText.setText(year + "-" + (month + 1) + "-" + day);
    }
}

单击ImageButton时调用此片段:

    /** Create DatePicker dialog on click of OrderDateEditText **/
   orderDatePickerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");
        }
    });

Month中有加 1,因为 DatePicker 默认将一月到十二月的月份从 0 到 11。

谢谢..

于 2013-05-16T04:58:11.113 回答
1

这是一个很好的模式:

  1. 在您的对话框中创建一个侦听器接口以处理对话框中的相关按钮按下
  2. 在启动活动中实现它
  3. 当用户按下“okay”或“cancel”时,调用监听器方法
  4. 在监听器的 impl 中做任何你需要做的事情来更新活动的视图

就像是,

class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
  static interface Listener {
    void onOkay(MyObject result);
    void onCancel();
  }
  ...

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    ...

    // set this as a listener for ok / cancel buttons
    return new AlertDialog.Builder(activity)...whatever...
        .setPositiveButton(R.string.ok, this)
        .setNegativeButton(R.string.cancel, this).create();
  }

  @Override
  public void onClick(DialogInterface dialog, int which) {
      if (which == DialogInterface.BUTTON_POSITIVE) {
          if (getActivity() instanceof Listener) {
              ((Listener)getActivity()).onOkay(...);
          }
      } else {
          if (getActivity() instanceof Listener) {
              ((Listener)getActivity()).onCancel();
          }
      }
  }
}

class MyActivity extends Activity implements MyDialogFragment.Listener {
  ...
  @Override
  public void onOkay(MyObject result) {
    // update activity view here with result
  }

  @Override
  public void onCancel() {
    // anything?
  }
}

如果您的情况,传递给方法的“结果”onOkay()将是用户在对话框中选择的 Date 对象。

于 2012-12-24T19:22:18.963 回答
0

AFragment不是View. 如果要查找片段,请使用findFragmentByIdfrom FragmentManager

于 2012-12-24T19:13:11.957 回答