所以我有一个按钮,我想将settext设置为在Dialog Fragment中选择的日期。我尝试过使用SharedPreferences,但除非重新启动片段,否则它不会更新。我试着看一下片段的生命周期,但我不认为它对Dialog Fragment的工作原理是一样的。所以我的问题是如何从片段
启动的对话框片段中设置片段的文本这是我的Dialog Fragment代码
public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), R.style.DatePickerTheme,this,year,month,day);
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String string = (i1 + 1) + "/" +i2 + "/" + i;
SharedPreferences.Editor editor = getActivity().getSharedPreferences("itemInfo", Context.MODE_PRIVATE).edit();
editor.putString("Birthday", string);
editor.apply();
}
}
这就是我正在尝试更改片段
中的文本mButton = view.findViewById(R.id.openCalendar);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment dialogFragment = new DatePickerDialogTheme();
dialogFragment.show(getActivity().getFragmentManager(), "Theme");
}
});
我也尝试在onResume期间更改文本,但仍然无法正常工作。当DialogFragment打开时,我认为它不会调用onResume。
答案 0 :(得分:0)
这是一种正确的方法:
定义接收结果的接口(请注意,我使用getChildFragmentManager
访问子片段的管理器,不要使用call getFragmentManager
public interface DatePickerResultHandler {
void onDatePicked(DatePickerDialogTheme dialog, int year, int month, int day);
}
让外部Fragment
实现接口以接收结果
public class TestFragment extends Fragment implements DatePickerResultHandler {
private Button mButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
mButton = view.findViewById(R.id.test_btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment dialogFragment = new DatePickerDialogTheme();
dialogFragment.show(getChildFragmentManager(), "Theme");
}
});
return view;
}
@Override
public void onDatePicked(DatePickerDialogTheme dialog, int year, int month, int day) {
String string = (year + 1) + "/" + month + "/" + day;
mButton.setText(string);
}
}
在DatePickerResultHandler
中找到DatePickerDialogTheme
。如果您仅将此对话框附加到Activity
,请先尝试使用Context
或Activity
进行投射。如果将其附加到父片段,请将其强制转换为getParentFragment
public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private boolean isAttach;
private DatePickerResultHandler mResultHandler;
@Override
public void onAttach(Context context) {
if (!isAttach) {
findResultHandler(context);
findResultHandler(getParentFragment());
isAttach = true;
}
super.onAttach(context);
}
@Override
public void onAttach(Activity activity) {
if (!isAttach) {
findResultHandler(activity);
findResultHandler(getParentFragment());
isAttach = true;
}
super.onAttach(activity);
}
private void findResultHandler(Object parent) {
if (mResultHandler == null && parent instanceof DatePickerResultHandler)
mResultHandler = (DatePickerResultHandler) parent;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
if (mResultHandler != null)
mResultHandler.onDatePicked(this, year, month, day);
dismiss();
}
}
答案 1 :(得分:0)
将您的TextView
对象传递到正在初始化它的片段广告的构造函数中,然后设置所需的文本。这可能是您尝试做的快速方法。
public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private TextView tv;
DatePickerDialogTheme(TextView tv){
this.tv = tv;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), R.style.DatePickerTheme,this,year,month,day);
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String string = (i1 + 1) + "/" +i2 + "/" + i;
tv.setText(string);
}
}
而另一部分 -
tv= parentViewOfTextView.findViewById(R.id.textView);
mButton = view.findViewById(R.id.openCalendar);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment dialogFragment = new DatePickerDialogTheme(tv);
dialogFragment.show(getActivity().getFragmentManager(), "Theme");
}
});
答案 2 :(得分:0)
感谢其他答案,但这对我来说效果最好。
我刚用过
Button button = getActivity().findViewById(R.id.calendarView);
button.setText(string);
在我的对话框片段中,它可以正常工作。