我需要将一个Activity
的RadioGroup
中选择的值传递给另一个的 5 个CheckBoxs
系列。我对全局变量的了解不够,无法自己做这件事
所以我在XML
的RadioGroup
中有 5 个RadioButtons
,就像这样:
<RadioButton
android:id="@+id/rdoInternet1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dedicated and Resilient"
android:layout_weight="1"/>
然后在我的Java中,我要去一系列嵌套的if语句
//find the id of the RadioGroup and store it in a variable
RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.radioInternet);
question1RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//set a listener on the RadioGroup
@Override
public void onCheckedChanged(RadioGroup view, @IdRes int checkedId) {
//checkedId refers to the selected RadioButton
//Perform an action based on the option chosen
if (checkedId == R.id.rdoInternet1) {
/*Nested if statement setting value 1 */
} else {
if (checkedId == R.id.rdoInternet2) {
/*Nested if statement setting value 1 */
} else {
if (checkedId == R.id.rdoInternet3) {
/*Nested if statement setting value 1 */
} else {
if (checkedId == R.id.rdoInternet4) {
/*Nested if statement setting value 1 */
} else {
if (checkedId == R.id.rdoInternet5) {
/*Nested if statement setting value 1 */
但是我不知道如何将其存储为全局变量,然后在下一个屏幕中再次使用嵌套 ifs 来调用它,以说出要检查的checkbox
。
所以我需要知道的是,有没有更好的方法来做到这一点以及如何存储全局价值
您所
要做的就是添加布尔变量作为意图额外:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("yourBoolName", true);
然后在您的下一个活动中获取它:
Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");