DatePickerDialog 结果返回到 Fragment?
Posted
技术标签:
【中文标题】DatePickerDialog 结果返回到 Fragment?【英文标题】:DatePickerDialog result back to Fragment? 【发布时间】:2018-11-02 23:56:14 【问题描述】:我想将 DatePickerDialog 中的选定日期返回到我的 Fragment。 当我测试它时,我只在构建日志中得到这个错误: E/ViewRootImpl: sendUserActionEvent() mView == null
在三星 Galaxy S6 Edge (7.0) 上测试我的代码
出现对话框,我可以选择一个日期,点击确定,但没有将结果显示到我的 TextView...我忘记了一些细节吗??
我调用 DatePickerDialog 的片段:
package at.marcelklein.drautaxi;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Calendar;
/**
* Created by Marcel on 10.09.2017.
*/
public class FourthFragment extends Fragment implements DatePickerDialog.OnDateSetListener
View myView;
TextView textView;
Button btnDayStart, btnDayEnd;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
myView = inflater.inflate(R.layout.fourth_layout, container, false);
btnDayStart = (Button) myView.findViewById(R.id.button_daystart);
btnDayEnd = (Button) myView.findViewById(R.id.button_dayend);
textView = (TextView) myView.findViewById(R.id.textView12);
btnDayStart.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DialogFragment datePicker = new DatePickerFragment();
FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
datePicker.show(ft, "datepicker");
);
btnDayEnd.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//showDialog();
);
return myView;
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
textView.setText(currentDateString);
DatePickerDialog 片段:
package at.marcelklein.drautaxi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
DatePickerDialog.OnDateSetListener onDateSet;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
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(), onDateSet, year, month, day);
提前致谢!
【问题讨论】:
【参考方案1】:您忘记将片段指定为侦听器。像这样添加它作为监听器
btnDayStart.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DialogFragment datePicker = new DatePickerFragment();
datePicker.onDateSet = FourthFragment.this
FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
datePicker.show(ft, "datepicker");
);
【讨论】:
我在 (public class FourthFragment extends Fragment implements DatePickerDialog.OnDateSetListener) 处实现了监听器。您的建议 (datePicker.onDateSet = FourthFragment.this) 无效:( 错误是什么。您需要在 DatePickerFragment 中公开 onDateSet 如果我点击运行,它会告诉我:在“FourthFragment”datePicker 中找不到符号变量 onDateSet。 onDateSet = FourthFragment.this; onDateSet 标记为红色...在 DatePickerFragment.class 我设置了这个: public DatePickerDialog.OnDateSetListener onDateSet;【参考方案2】:我将代码更改为:DatePickerDialog
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
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);
@Override
public void onDateSet(DatePicker view, int year, int month, int day)
TextView tv = (TextView) getActivity().findViewById(R.id.textView12);
DecimalFormat df = new DecimalFormat("00");
tv.setText("Gewähltes Datum:");
tv.setText(tv.getText() + "\nJahr: " + year);
tv.setText(tv.getText() + "\nMonat: " + df.format(month));
tv.setText(tv.getText() + "\nTag: " + df.format(day));
----------- 还有我的 Fragment 我称之为 DatePickerDialog:
public class FourthFragment extends Fragment
View myView;
public TextView textView;
Button btnDayStart, btnDayEnd;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
myView = inflater.inflate(R.layout.fourth_layout, container, false);
btnDayStart = (Button) myView.findViewById(R.id.button_daystart);
btnDayEnd = (Button) myView.findViewById(R.id.button_dayend);
textView = (TextView) myView.findViewById(R.id.textView12);
btnDayStart.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getFragmentManager(), "date Picker");
);
return myView;
现在我直接从 DatePickerDialog 更改 TextView。 我认为这不是最好的解决方案...
我想要的是一个范围日期选择器,如“TimeSquare DatePicker”。 但我不能在“TimeSquare DatePicker”中选择过去的日子。
有什么建议吗?
【讨论】:
以上是关于DatePickerDialog 结果返回到 Fragment?的主要内容,如果未能解决你的问题,请参考以下文章
旋转时调用 DatePickerDialog onDateSet
在 android DatePickerDialog 中将语言设置为法语