如何在android中显示两个不同的日期选择器

时间:2015-04-23 07:32:34

标签: android datepicker

嗨,在下面我显示两个日期选择器,一个是fordate,另一个是for date。但是我采用了两个不同的方法,我正在调用。但是使用单个函数如何显示两个不同的日期选择器选择日期

任何人都可以帮助我

预期产出:

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();
        }

    }

1 个答案:

答案 0 :(得分:0)

在两个不同的按钮中设置两个不同的日期选择器。例如,如果你按下显示日期选择器弹出窗口,当你按下日期然后显示另一个日期选择器..