在我的应用程序中,我有一个日期选择器,我能够在textview中选择并设置所选日期,但问题是如果再次点击textview打开datepicker dailog,它总是显示当前日期而不是最后选择日期..那么问题是什么?
public class MainActivity extends Activity {
private TextView date_dropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date_dropdown=(TextView)findViewById(R.id.shows_dt);
Calendar calendar = Calendar.getInstance();
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
date_dropdown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
}
public void showDatePickerDialog() {
System.out.println("show date picke dilg ");
System.out.println("show date picke dilg");
DialogFragment newFragment1 = new SelectDateFragment();
newFragment1.show(getFragmentManager(), "DatePicker");
}
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
public void populateSetDate(int year, int month, int day) {
date_dropdown.setText(day + "-" + month + "-" + year);
}
}
答案 0 :(得分:2)
以下线你已经获得当前日期, 所以你只会获得当前日期。
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
将您选择的日期保存在共享首选项中,然后在TextView中显示
答案 1 :(得分:1)
您应该在onCreate()
方法中定义日历实例,然后在全年的月份和日期值更新onDateSet()
,所以当您再次打开日期选择器对话框时,它将显示以前选择的日期。
就像这样
public class MainActivity extends Activity {
private TextView date_dropdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Calendar calendar = Calendar.getInstance();
int yy1 = calendar.get(Calendar.YEAR);
int mm1 = calendar.get(Calendar.MONTH);
int dd1 = calendar.get(Calendar.DAY_OF_MONTH);
date_dropdown=(TextView)findViewById(R.id.shows_dt);
Calendar calendar = Calendar.getInstance();
date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.YEAR));
date_dropdown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
}
public void showDatePickerDialog() {
System.out.println("show date picke dilg ");
System.out.println("show date picke dilg");
DialogFragment newFragment1 = new SelectDateFragment();
newFragment1.show(getFragmentManager(), "DatePicker");
}
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), this, yy1, mm1, dd1);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
yy1 = yy ;
mm1 = mm;
dd1 = dd ;
}
public void populateSetDate(int year, int month, int day) {
date_dropdown.setText(day + "-" + month + "-" + year);
}
}
答案 2 :(得分:0)
看起来每次点击都会创建一个新对话框,这意味着onCreateDialog
将日期再次设置为当前时间。您希望拥有一个类变量DialogFragment df
,并在showDatePickerDialog
中执行以下操作:
public void showDatePickerDialog() {
if (df == null){
// Only create a new fragment if one does not exist
DialogFragment df = new SelectDateFragment();
}
df.show(getFragmentManager(), "DatePicker");
}
您还需要存储用户在onPause
方法中选择的值,并将DialogFragment
设置为onResume
中的该值,以防您的用户离开您的应用时输入信息。
答案 3 :(得分:0)
How to disable future date picker in android
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
答案 4 :(得分:0)
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
答案 5 :(得分:0)
updateDate通过在日期选择器中设置上一个选定日期
mDatePicker.updateDate(1994, 6, 12);
mDatePicker.setTitle("Please select date");
mDatePicker.updateDate(1994, 6, 12);
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
答案 6 :(得分:0)
打印所选日期的简单方法是:
picker.addOnPositiveButtonClickListener {
tv_date.text = "picker.headerText"
}
picker
是 DatePicker 类的对象。