0
您好在下面我顯示兩個日期選擇器一個是fromdate和另一個date.but爲此,我採取了兩種不同的方法,我打電話。但使用單個函數如何顯示兩個不同的日期選擇器選擇日期如何在android中顯示兩個不同的日期選擇器
任何一個可以幫助我
預期輸出:
for example
fromdate:2015-04-01
todate:2015-04-23
的Java
public class General_report extends Activity implements OnClickListener{
EditText fromDate,toDate;
DatePickerDialog fromDatePickerDialog;
DatePickerDialog toDatePickerDialog;
SimpleDateFormat dateFormatter;
Button report;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_report);
report=(Button)findViewById(R.id.report);
report.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String selectedFromDate=fromDate.getText().toString();
String selectedToDate=toDate.getText().toString();
Intent i = new Intent(getApplicationContext(),General_patient_list.class);
i.putExtra("date1", selectedFromDate);
i.putExtra("date2", selectedToDate);
startActivity(i);
}
});
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
findViewsById();
findViewsById1();
setDateTimeField();
setDateTimeField1();
}
private void findViewsById() {
fromDate = (EditText) findViewById(R.id.etxt_fromdate);
fromDate.setInputType(InputType.TYPE_NULL);
fromDate.requestFocus();
}
private void findViewsById1() {
toDate = (EditText) findViewById(R.id.etxt_todate);
toDate.setInputType(InputType.TYPE_NULL);
toDate.requestFocus();
}
private void setDateTimeField() {
fromDate.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this,
new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDate.setText(dateFormatter.format(newDate
.getTime()));
}
},
newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
}
private void setDateTimeField1() {
toDate.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
toDatePickerDialog = new DatePickerDialog(this,
new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
toDate.setText(dateFormatter.format(newDate
.getTime()));
}
},
newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
}
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onClick(View view) {
if (view == fromDate) {
fromDatePickerDialog.show();
}
if (view == toDate) {
toDatePickerDialog.show();
}
}