当我按下某个活动上的按钮时,会调用我的DialogFragment类。我希望通过此DialogFragment设置的日期显示在按钮文本上。 FindViewById拒绝被类识别。
我找到了similar question,但我无法将其与我的案例联系起来。
代码:
调用Dialogue片段的按钮:
public void datepicker(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
对话片段代码:
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
}
}
}
答案 0 :(得分:10)
在DialogFragment中负责在用户设置日期时收到通知的方法中,执行此操作
Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);
答案 1 :(得分:2)
为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");
}
});
在月中有加1,因为DatePicker默认在1月到12月之间需要从0到11的月份。
谢谢..
答案 2 :(得分:1)
这是一个很好的模式:
之类的,
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对象。
答案 3 :(得分:0)
Fragment
不是View
。如果要查找片段,请使用findFragmentById
中的FragmentManager
。