gpt4 book ai didi

Android - 在不同的 Activity 中使用一个 DatePicker fragment

转载 作者:行者123 更新时间:2023-11-30 03:17:19 31 4
gpt4 key购买 nike

有没有可能只有一个 DatePickerFragment,可以在不同的 Activity 中使用?我的 DatePickerFragment:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

final Calendar cal = new GregorianCalendar();
int year;
int month;
int day;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker

year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);

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

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
updateDate(year, month, day);
}

public void updateDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
cal.set(year, month, day);
}

}

我找到了 here那与

return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);

应该可以在 Activiy 中实现 onDateSet。

public class MyActivity extends FragmentActivity implements DatePickerDialog.OnDateSetListener {
...
public void changeDate(View v) {
log("Changing Date");
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getFragmentManager(), "datePicker");
}
public void onDateSet(DatePicker view, int year, int month, int day) {
log(String.format("Date changed: %d-%d-%d", year, month, day));
}

但是onDateSet 不会被执行。我的错误是什么?我只想要一个可重复使用的 fragment ,我可以在不同的 Activity 中使用它,而无需在每个 Activity 中编写相同的代码。

最佳答案

为此,我还创建了一个扩展 DialogFragment 的 DatePickerFragment。在这个类中,我使用了从父类传递的布局 ID,如下所示:

    @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setStyle(DialogFragment.STYLE_NO_TITLE, 0);
setCancelable(false);

Bundle args = getArguments();
id = args.getString("id");
title = args.getString("title");
layout = args.getInt("layout");
message = args.getString("msg");

}

之后

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

View view = inflater.inflate(layout, null);


switch (layout) {

case R.layout.date_picker:
titleTxt = (TextView) view.findViewById(R.id.dtedlg_tv_heading);
datePkr = (DatePicker) view.findViewById(R.id.dtedlg_dp_date);
done = (Button) view.findViewById(R.id.dtedlg_btn_done);
cancel = (Button) view.findViewById(R.id.dtedlg_btn_back);
break;
}

done.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (id.equals(ParentFragment.DATE_DIALOG)) { // checking

Calendar value = Calendar.getInstance();
value.set(Calendar.DAY_OF_MONTH, datePkr.getDayOfMonth());
value.set(Calendar.MONTH, datePkr.getMonth());
value.set(Calendar.YEAR, datePkr.getYear());

ParentFragment.from_date.setText(DateFormat
.format("dd/MM/yyyy", value).toString());
}
getDialog().dismiss();
}
}
return view;
}

遵循“date_picker”布局:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:orientation="vertical" >

<LinearLayout
style="@style/DialogBanner.Solid.Apptheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="40dp"
android:orientation="vertical" >

<TextView
android:id="@+id/dtedlg_tv_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
</LinearLayout>

<DatePicker
android:id="@+id/dtedlg_dp_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
style="@style/Dialog_LayoutFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >

<Button
android:id="@+id/dtedlg_btn_done"
style="@style/App_ButtonStyleFooter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/ic_action_done"
android:minHeight="10dp"
android:minWidth="10dp" />

<Button
android:id="@+id/dtedlg_btn_back"
style="@style/App_ButtonStyleFooter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:drawableRight="@drawable/ic_action_back"
android:minHeight="10dp"
android:minWidth="10dp" />
</LinearLayout>

</LinearLayout>

关于Android - 在不同的 Activity 中使用一个 DatePicker fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19808196/

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