简体   繁体   中英

I want to pass Date and Time I got using Datepicker Dialog and Timepicker Dialog to next Activity in Android studio

This is the Java code that Datepicker and Timepicker work. Those two are working properly so I want to send that selected date and time to the next activity( Doctor_Time_Picking_data_page.java )

 Doctor_Time_Picking_page.java
     public class Doctor_Time_Picking_page extends AppCompatActivity {
        public static final String TEXT_TO_SEND ="com.example.dogapp.TEXT_TO_SEND";
        private DatePickerDialog datePickerDialog;
        private Button dateButton;
        private Button timeButton;
        private Bundle savedInstanceState;
        private Button saveButton;
        private String DATE;
    
    
        //On Create method-------------------------------------------------------------------------------------------------------------------------
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_doctor_time_picking_page);
            initDatePicker();
            dateButton = findViewById(R.id.datePickerButton);
            timeButton = findViewById(R.id.timeButton);
            saveButton =  findViewById(R.id.date_time_save_button);
    
     
            Intent intent = new Intent(getApplicationContext(),Doctor_Time_Picking_data_page.class);
     
            saveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(intent);
                }
            });
    
    

        }
    
    //-----------------------------------------------------------------------------------------------------------------------------------
        private Bundle initDatePicker()
        {
    
            DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
            {
                @Override
                public void onDateSet(DatePicker datePicker, int year, int month, int day)
                {
                    month = month + 1;
                    String date = makeDateString(day, month, year);
                   dateButton.setText(date);
    
    
    
                }
            };
    
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);
    
            int style = AlertDialog.THEME_HOLO_LIGHT;
    
            datePickerDialog = new DatePickerDialog(this, style, dateSetListener, year, month, day);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    
            return null;
        }
    
        private String makeDateString(int day, int month, int year)
        {
            return getMonthFormat(month) + " " + day + " " + year;
        }
    
        private String getMonthFormat(int month)
        {
            if(month == 1)
                return "JAN";
            if(month == 2)
                return "FEB";
            if(month == 3)
                return "MAR";
            if(month == 4)
                return "APR";
            if(month == 5)
                return "MAY";
            if(month == 6)
                return "JUN";
            if(month == 7)
                return "JUL";
            if(month == 8)
                return "AUG";
            if(month == 9)
                return "SEP";
            if(month == 10)
                return "OCT";
            if(month == 11)
                return "NOV";
            if(month == 12)
                return "DEC";
            //default should never happen
            return "JAN";
        }
    
        public void openDatePicker(View view)
        {
            datePickerDialog.setTitle("Select Date");
            datePickerDialog.show();
        }
    
    
    
    
        //Time Button
    
    
        int hour, minute;
    
    
        public void popTimePicker(View view)
        {
            TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener()
            {
    
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute)
                {
                    hour = selectedHour;
                    minute = selectedMinute;
                    timeButton.setText(String.format(Locale.getDefault(), "%02d:%02d",hour, minute));
                }
            };
            TimePickerDialog timePickerDialog = new TimePickerDialog(this, /*style,*/ onTimeSetListener, hour, minute, true);
    
            timePickerDialog.setTitle("Select Time");
            timePickerDialog.show();
        }
    
    
    }
    

This is the page where I want to show the date and Time selected form my previous activity(Doctor_Time_Picking_page.java)

Doctor_Time_Picking_data_page.java : public class Doctor_Time_Picking_data_page extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_doctor_time_picking_data_page);
    

            Button Button1a = findViewById(R.id.doc_page4_btn1);
            Button Button2a = findViewById(R.id.doc_page4_btn2);
            Button Button3a = findViewById(R.id.doc_page4_btn3);
            Dialog nDialog = new Dialog(this);
    
            
            Button1a.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_Appoinment_payment.class);
                    startActivity(intent);
                }
            });
    
            Button2a.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_Time_Picking_page.class);
                    startActivity(intent);
                }
            });
    
            Button3a.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    nDialog.setContentView(R.layout.activity_doctor_delete_popup_msg);
                    nDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    Intent intent = new Intent(Doctor_Time_Picking_data_page.this,Doctor_delete_popup_msg.class);
                    startActivity(intent);
                }
            });
    
        }
    }

In my project there are three buttons those are Select Time, Select Date and save button when I select each button Datepicker and Timepicker dialogues appear when I select Date or time those Data is appearing on the Buttons I want to pass those data to my next activity which is Doctor_Time_Picking_data_page.java and display them to user. What I now want is I want to pass That data selected from those Pickers to next activity

You need to pass an intent extra to the other activity to get your result. Follow the steps:

  1. Create a filed name date in your class:
private String date = "JAN 1 2022"; // I have given a sample date
  1. You need to store the date inside the DateSetListener like this:
 DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
            {
                @Override
                public void onDateSet(DatePicker datePicker, int year, int month, int day)
                {
                    month = month + 1;
                    date = makeDateString(day, month, year);
                    dateButton.setText(date);
    
    
    
                }
            };
  1. You need to pass that date on the click of the save button:
saveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    intent.putExtra("date", date);
                    startActivity(intent);
                }
            });
  1. Then you need to get that date on the other activity like this:
public class Doctor_Time_Picking_data_page extends AppCompatActivity {

        ...
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_doctor_time_picking_data_page);
    
               String date = getIntent().getStringExtra("date", "");
    
               // 👆 that is the date from the previous activity
          
               ...
            });
            
            ...

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM