简体   繁体   中英

How can I access a component(for example a timepicker) in one activity from another activity in Android?

I want to access a timepicker in an activity from another activity in the same app. For example, if I change the time in the timepicker in one activity, the time in the timepicker in the other activity should also change accordingly. How can I do this?

I guess if i am right , you may like to pass your date attributes to next activity using an Intent , and then set the state of the timepicker with those values in next activity . Here is a sample , what you may/should do i.putExtra("day", day); i.putExtra("month", month); i.putExtra("year", year); i.putExtra("day", day); i.putExtra("month", month); i.putExtra("year", year); . If you are not familiar with intents , have a look here https://developer.android.com/reference/android/content/Intent.html

You can use putExtra to pass data to your new intent.

int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
String time = hour + ":" + minute;

Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra(time, "timeData");
startActivity(intent)

To view the time.

Bundle extras = getIntent().getExtras();
if (extras != null) {
   String value = extras.getString("timeData");
   //do stuff with the time now...
}

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