gpt4 book ai didi

android - 从 fragment 调用日期选择器

转载 作者:行者123 更新时间:2023-11-29 21:04:08 26 4
gpt4 key购买 nike

这是我尝试使用 DialogFragment 调用 datepicker 的 fragment 代码

public class ExpensesDaily extends Fragment{

public ExpensesDaily(){}
Button button;
Calendar now;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);

Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
return rootView;
}

}

这是我的 DatePickerFragment 代码

public class DatePickerFragment extends DialogFragment  {

DatePickerDialog.OnDateSetListener onDateSet;
TextView tvDate;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
tvDate = (TextView) rootView.findViewById(R.id.textViewDate);

return rootView;


}
public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
onDateSet = onDate;
}

public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
tvDate.setText("date: " + formattedDate);
}

}

但每次我单击按钮以显示日期选择器时,应用程序都会崩溃。有什么地方还不完整吗?

更新:我正在尝试在此处执行本文 http://www.vogella.com/tutorials/AndroidFragments/article.html , 但找不到适合日期选择器的方法

谢谢

最佳答案

好的,这就是我的做法。我将我的整个代码粘贴在这里以帮助任何与我有同样问题的人。我在这里引用这篇文章http://www.solardriftsolutions.co.uk/android-fragment-and-dialogfragment/修复 requestFeature() 错误和此处的文章 http://androidtrainningcenter.blogspot.com/2012/10/creating-datepicker-using.html获取日期选择器设置的值

扩展 DialogFragment 的 DatePickerFragment.java 代码

public class DatePickerFragment extends DialogFragment  {

DatePickerDialog.OnDateSetListener onDateSet;
private boolean isModal = false;

public static DatePickerFragment newInstance()
{
DatePickerFragment frag = new DatePickerFragment();
frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
return frag;
}

public DatePickerFragment(){}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Use the current date as the default date in the picker
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);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(isModal) // AVOID REQUEST FEATURE CRASH
{
return super.onCreateView(inflater, container, savedInstanceState);
}
else {
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}

public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
onDateSet = onDate;
}

}

这里是 ExpensesDaily.java 的代码,它是一个调用 datepickerfragment 的 fragment

public class ExpensesDaily extends Fragment{

public ExpensesDaily(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);

Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment dpf = new DatePickerFragment().newInstance();
dpf.setCallBack(onDate);
dpf.show(getFragmentManager().beginTransaction(), "DatePickerFragment");
//DialogFragment picker = new DatePickerFragment();
//picker.show(getFragmentManager(), "datePicker");
}
});
return rootView;
}

DatePickerDialog.OnDateSetListener onDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {

TextView tvDate = (TextView) getActivity().findViewById(R.id.tvDate);
tvDate.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear)
+ "-" + String.valueOf(dayOfMonth));
}
};

}

关于android - 从 fragment 调用日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25065307/

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