我在对话框中使用两个文本视图,一个用于日期,另一个用于日期。现在,当我从日期文本视图中单击时,日期选择器对话框将打开,并且当我在文本视图中选择未更新的日期时,如果我再次打开日期选择器并在第二次选择日期时更新日期文字检视。有谁能弄清楚为什么不第一次更新它。
*.csproj
答案 0 :(得分:0)
您可以这样尝试吗?
private static void setDate() {
try {
dateFrom = simpleDateFormat.parse(fromDate);
dateTo = simpleDateFormat.parse(toDate);
fromDateText.setText(dateFrom.toString());
toDateText.setText(dateTo.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
//编辑:
检查我的代码:
private void showDatePicker() {
final Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
setDate(myCalendar.getTime());
}
};
if (getActivity() != null) {
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
private void setDate(Date time) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
editText.setText(sdf.format(time));
}
答案 1 :(得分:0)
尝试此代码.. 将布尔变量用于选定的textview ...
这样的更改方法。
public class DialogActiivty extends AppCompatActivity {
private TextView fromDateText,toDateText;
private String fromDate,toDate;
private Dialog dialog;
private DatePickerDialog fromDatePicker;
private SimpleDateFormat simpleDateFormat;
private Calendar calendar;
private int year, month, day;
private boolean fromSelected=false,toSelected=true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datePickerDialog(DialogActiivty.this);
}
public void datePickerDialog(final Context context) {
calendar=Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
dialog = new Dialog(context);
dialog.setContentView(R.layout.date_picker_dialog);
fromDateText = dialog.findViewById(R.id.from_date);
toDateText = dialog.findViewById(R.id.to_date);
fromDateText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toSelected=false;
fromSelected=true;
datePicker(context);
}
});
toDateText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fromSelected=false;
toSelected=true;
datePicker(context);
}
});
dialog.show();
// fromDateText.setText(fromDate);
// toDateText.setText(toDate);
}
public void datePicker(Context context){
fromDatePicker = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth
,fromDateListner, year, month, day);
simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
fromDatePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
fromDatePicker.show();
}
DatePickerDialog.OnDateSetListener fromDateListner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month+=1;
fromDate = dayOfMonth+"/"+month+"/"+year;
setDate(fromDate);
}
};
private void setDate(String fromDate) {
if (fromSelected) {
fromDateText.setText(fromDate);
}
if (toSelected) {
toDateText.setText(fromDate);
}
}
}
答案 2 :(得分:0)
fromDateListner
在创建对话框后初始化,因此,第一次创建DatePickerDialog
时没有监听器。
将fromDateListner = new DatePickerDialog.OnDateSetListener() ...
部分移至fromDatePicker = new DatePickerDialog(context ...