问题
i have an activity showing a dialog which contains radioButtons, and to buttons (Ok,cancel)
- can i launch an activity from this dialog ?
- how to send the id of the radioButton selected to the second activity ?
回答1:
in the onClick()
method of the OK Button, you can add this code to start another Activity and pass the id of the checked radio button :
Intent intent = new Intent(FirstActivity.this, SecondActitvity.class);
intent.putextras("extra_selected_radio_button", checkedRadioButton.getId());
startActivity(intent);
and in the onCreate()
method of the SecondActivity
, you can retrieve the id of the selected radio button like this :
Intent intent = getIntent();
int radioButtonId = intent.getIntExtra("extra_selected_radio_button", -1); // -1 is the default value
PS : Check this tutorial to learn more about sending data between activities in Android.
回答2:
Yes you can:
- On the OK button listener just do
startActivity(intent)
- Before the previous method call you have to put an extra in the intent, like so
intent.putExtra("radioButtonId", radioButtonValue)
回答3:
To start an activity from your dialog you'll have to add this into your button's onClick() code:
Intent intentLoad = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult();
If you want to send data to your activity, use: putExtra
Like this:
Intent intentLoad = new Intent(getBaseContext(), YourNewActivity.class);
intent.putExtra("radioID", id-of-your-radioButton);
startActivityForResult();
You can catch your id in the activity with:
int myRadioId = extras.getInt("radioID");
来源:https://stackoverflow.com/questions/22874408/sending-data-to-an-intent-depending-on-a-view-a-radio-button