如何设置两个或更多DatePicker并在TextView上显示它?

时间:2016-09-15 18:26:34

标签: android datepicker

我在单个屏幕中设置了三个DatePicker。它工作正常,但问题是,当我在该tym日期设置一个DatePicker时,在所有其他日期选择器的Textview中设置。

此处 Main.Java

    package com.example.sachin.datepicker;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

        EditText openingdate,Birthdate,Anniversary;
        Calendar calendar ;
        DatePickerDialog datePickerDialog,datePickerDialog1 ;
        int Year, Month, Day ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                openingdate = (EditText)findViewById(R.id.openingdate);
                Birthdate = (EditText)findViewById(R.id.Birthdate);
                Anniversary = (EditText)findViewById(R.id.Anniversary);

                calendar = Calendar.getInstance();
                Year = calendar.get(Calendar.YEAR) ;
                Month = calendar.get(Calendar.MONTH);
                Day = calendar.get(Calendar.DAY_OF_MONTH);

                Birthdate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                datePickerDialog1 = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);
                                datePickerDialog1.setThemeDark(false);
                                datePickerDialog1.showYearPickerFirst(false);
                                datePickerDialog1.setAccentColor(Color.parseColor("#009688"));
                                datePickerDialog1.setTitle("Select Date From DatePickerDialog");
                                datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
                        }
                });
                openingdate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);
                                datePickerDialog.setThemeDark(false);
                                datePickerDialog.showYearPickerFirst(false);
                                datePickerDialog.setAccentColor(Color.parseColor("#009688"));
                                datePickerDialog.setTitle("Select Date From DatePickerDialog");
                                datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
                        }
                });
        }
        @Override
        public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {
                String date = "Selected Date : " + Day + "-" + Month + "-" + Year;
                openingdate.setText(date);
                String date1 = "Selected Date : " + Day + "-" + Month + "-" + Year;
                Birthdate.setText(date1);
                Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
        }



}

我应该在这里改变什么,

  @Override
    public void onDateSet (DatePickerDialog view,int Year, int Month, int Day){
                    String date1 = "Selected Date : " + Day + "-" + Month + "-" + Year;
                    Birthdate.setText(date1);
                    Toast.makeText(MainActivity.this, date1, Toast.LENGTH_LONG).show();
            }



    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

            String date1 = "Selected Date : " + Day + "-" + Month + "-" + Year;
            Birthdate.setText(date1);
            Toast.makeText(MainActivity.this, date1, Toast.LENGTH_LONG).show();
    }

我尝试过这种方式,但是当我选择一个日期时,它会在其他Datepicker的TextView中设置。

1 个答案:

答案 0 :(得分:2)

您需要制作2个单独的DatePicker对话框

制作2个听众

int from_year, from_month, from_day,to_year, to_month, to_day; //initialize them to current date in onStart()/onCreate()
DatePickerDialog.OnDateSetListener from_dateListener,to_dateListener;

然后实施

 from_dateListener = new OnDateSetListener(){

            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

                          ...

                }
            }

        };
        to_dateListener = new OnDateSetListener(){
            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                .....
            }
        };