两个日期选择器片段 - 如何分别编辑日期?
Two Date Picker fragments - how to edit date separately?
我正在学习日期选择器 Android 教程。不幸的是,在这样做之后,我意识到它只适用于我的一个文本字段,老实说,我不知道如何将它应用到另一个文本字段,DateEdit2。也就是说,单击 DateEdit1 或 DateEdit2 我编辑 DateEdit1。我知道这是来自设置日期的功能,但是任何尝试更改或将按钮作为参数传递都会导致错误,因为这些方法无法以这种方式进行编辑。有什么想法吗?
代码如下:
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit1.setText(DateEdit1.getText() + " " + day + "/" + (month + 1) + "/" + year);
}
}
提前谢谢你,
约翰
在 activity 中创建全局 public boolean isDateEdit1;
。
在 DateEdit1
的 onClickListener
中,在调用 showTruitonDatePickerDialog(View v)
之前,设置 isDateEdit1 = true;
。
在 DateEdit2
的 onClickListener
中,在调用 showTruitonDatePickerDialog(View v)
之前,设置 isDateEdit1 = false;
。
然后,在 onDateSet
:
public void onDateSet(DatePicker view, int year, int month, int day) {
if(getActivity.isDateEdit1){
DateEdit1.setText(""+day + "/" + (month + 1) + "/" + year);
return;
}
DateEdit2.setText("" + day + "/" + (month + 1) + "/" + year);
}
我创建了另一个 class,它使用另一个名称扩展了 DialogFragment
,并使用了不同的函数:
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void datePickerDialog(View v) {
DialogFragment newFragment = new SecondDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker1");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit1.setText(DateEdit1.getText() + " "
+ day + "/" + (month + 1) + "/" + year);
}
}
public static class SecondDatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit2.setText(DateEdit2.getText() + " "
+ day + "/" + (month + 1) + "/" + year);
}
}
它不是很好,但对我有用。
因此,在第一个 onClickListener()
上调用 showTruitonDatePickerDialog()
,然后在第二个 onClickListener()
上调用 datePickerDialog()
。
我正在学习日期选择器 Android 教程。不幸的是,在这样做之后,我意识到它只适用于我的一个文本字段,老实说,我不知道如何将它应用到另一个文本字段,DateEdit2。也就是说,单击 DateEdit1 或 DateEdit2 我编辑 DateEdit1。我知道这是来自设置日期的功能,但是任何尝试更改或将按钮作为参数传递都会导致错误,因为这些方法无法以这种方式进行编辑。有什么想法吗?
代码如下:
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit1.setText(DateEdit1.getText() + " " + day + "/" + (month + 1) + "/" + year);
}
}
提前谢谢你, 约翰
在 activity 中创建全局 public boolean isDateEdit1;
。
在 DateEdit1
的 onClickListener
中,在调用 showTruitonDatePickerDialog(View v)
之前,设置 isDateEdit1 = true;
。
在 DateEdit2
的 onClickListener
中,在调用 showTruitonDatePickerDialog(View v)
之前,设置 isDateEdit1 = false;
。
然后,在 onDateSet
:
public void onDateSet(DatePicker view, int year, int month, int day) {
if(getActivity.isDateEdit1){
DateEdit1.setText(""+day + "/" + (month + 1) + "/" + year);
return;
}
DateEdit2.setText("" + day + "/" + (month + 1) + "/" + year);
}
我创建了另一个 class,它使用另一个名称扩展了 DialogFragment
,并使用了不同的函数:
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void datePickerDialog(View v) {
DialogFragment newFragment = new SecondDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker1");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit1.setText(DateEdit1.getText() + " "
+ day + "/" + (month + 1) + "/" + year);
}
}
public static class SecondDatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit2.setText(DateEdit2.getText() + " "
+ day + "/" + (month + 1) + "/" + year);
}
}
它不是很好,但对我有用。
因此,在第一个 onClickListener()
上调用 showTruitonDatePickerDialog()
,然后在第二个 onClickListener()
上调用 datePickerDialog()
。