sending data to an intent depending on a view (a radio button)

只谈情不闲聊 提交于 2019-12-12 01:45:54

问题


i have an activity showing a dialog which contains radioButtons, and to buttons (Ok,cancel)

  1. can i launch an activity from this dialog ?
  2. 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:

  1. On the OK button listener just do startActivity(intent)
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!