因此,当DatePickerFragment
为空时,我为用户创建EditText
以在EditText
行上输入日期:
@Override
public void onClick(View v) {
if (xEditText.getText().length() == 0) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
稍后,如果EditText
不为空(因为用户先前输入了日期),那么我想将包中的日期发送到片段:
else {
...
Bundle argsbundle = new Bundle();
argsbundle.putInt("year", c.get(Calendar.YEAR));
...
我的问题是我应该在DatePickerFragment
声明中使用之前命名的newFragment
创建else {
的新实例:
else {
...
Bundle argsbundle = new Bundle();
argsbundle.putInt("year", c.get(Calendar.YEAR));
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setArguments(argsbundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
或者我应该创建一个新命名的片段,与newFragment
不同:
else {
...
Bundle argsbundle = new Bundle();
argsbundle.putInt("year", c.get(Calendar.YEAR));
DatePickerFragment secondFragment = new DatePickerFragment();
secondFragment.setArguments(argsbundle);
secondFragment.show(getSupportFragmentManager(), "datePicker");