gpt4 book ai didi

android - 旋转时调用 DatePickerDialog onDateSet

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:55 26 4
gpt4 key购买 nike

我正在使用 DialogFragment 在 onCreateDialog() 中返回 DatePickerDialog。我已将 dateSetListener 设置为 DialogFragment(在下面的示例中为“this”)并且一切正常,除了在发生屏幕旋转时调用 onDateSet(),这是不希望的。如何让 onDateSet 在屏幕旋转时不被调用?

我的 DialogFragment

public static class DateDialogFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener{

public static DateDialogFragment newInstance() {
return new DateDialogFragment();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), this, 2012, 11, 19);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
//This is called when screen rotated, which I dont want
Toast.makeText(getActivity(), "Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth, Toast.LENGTH_SHORT).show();
}

}

我就是这样调用它的

if(getActivity()!=null){
FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment newFragment = DateDialogFragment.newInstance();
newFragment.show(fm, "dialog");
}

最佳答案

onDateSet 方法中,使用Activity::isChangingConfigurations 检查Activity 是否因配置更改而重新启动。 .如果是,则不显示 Toast

@Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if( ! this.getActivity().isChangingConfigurations() ) {
// Dialog is dismissed by user explicitly, hence show the Toast message.
Toast.makeText(getActivity(), "Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth, Toast.LENGTH_SHORT).show();
}
}

我已经对其进行了测试并且运行良好。如果需要任何进一步的帮助,请告诉我。

关于android - 旋转时调用 DatePickerDialog onDateSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13959164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com