RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { Toast.makeText(MainActivity.this, checkedId == R.id.radioButton_boy?"生男孩":"生女孩", Toast.LENGTH_SHORT).show(); } });
<RadioGroup android:orientation="horizontal" android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioButton android:text="男孩" android:id="@+id/radioButton_boy" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="女孩" android:id="@+id/radioButton_girl" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup>
时间: 2024-10-08 20:44:57