在两个类中使用一个DatePickerDialog可以通过以下步骤实现:
public class CustomDatePickerDialog extends DatePickerDialog implements DatePickerDialog.OnDateSetListener {
private OnDateSetListener mListener;
public CustomDatePickerDialog(Context context, OnDateSetListener listener, int year, int month, int dayOfMonth) {
super(context, listener, year, month, dayOfMonth);
mListener = listener;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mListener.onDateSet(view, year, month, dayOfMonth);
}
}
public class ClassA extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
// ...
private void showDatePickerDialog() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
CustomDatePickerDialog datePickerDialog = new CustomDatePickerDialog(this, this, year, month, dayOfMonth);
datePickerDialog.show();
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// 处理日期选择后的逻辑
}
}
public class ClassB extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
// ...
private void showDatePickerDialog() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
CustomDatePickerDialog datePickerDialog = new CustomDatePickerDialog(this, this, year, month, dayOfMonth);
datePickerDialog.show();
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// 处理日期选择后的逻辑
}
}
通过以上步骤,你可以在两个类中使用同一个自定义的DatePickerDialog,并分别处理日期选择后的逻辑。
领取专属 10元无门槛券
手把手带您无忧上云