You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何获取选中RadioButton文本并在另一个Activity展示?

Solution to Retrieve Selected RadioButton Text from RadioGroup

No worries, this is a common hiccup when working with RadioGroups in Android—let’s break down exactly how to fix it so you can get that gender text to your second Activity smoothly.

Step 1: Get the Selected RadioButton from Your RadioGroup

First, in your main input Activity, you’ll need to reference your RadioGroup and pull the text from the checked option. Here’s the code to do that:

// Grab the RadioGroup instance using its ID
RadioGroup sexRadioGroup = findViewById(R.id.sex);

// Get the ID of the currently selected RadioButton
int selectedRadioId = sexRadioGroup.getCheckedRadioButtonId();

// Find the actual RadioButton view using that ID
RadioButton selectedRadioButton = findViewById(selectedRadioId);

// Extract the text as a String (add a null check to avoid crashes)
String selectedGender = "";
if (selectedRadioButton != null) {
    selectedGender = selectedRadioButton.getText().toString();
}

The null check is key here—it prevents a crash if the user doesn’t select any gender option. You could even add a toast message here to prompt them to pick a gender if needed.

Step 2: Pass the Gender Text to the Second Activity

Once you have the selectedGender string, include it in the Intent you use to launch your detail Activity, just like you do with name, surname, and email:

// Example Intent setup (adjust to match your existing code)
Intent intent = new Intent(MainActivity.this, UserDetailActivity.class);
intent.putExtra("USER_NAME", nameEditText.getText().toString());
intent.putExtra("USER_SURNAME", surnameEditText.getText().toString());
intent.putExtra("USER_EMAIL", emailEditText.getText().toString());
intent.putExtra("USER_GENDER", selectedGender); // Add this line to pass gender
startActivity(intent);

Step 3: Display the Gender in the Second Activity

In your detail Activity, retrieve the gender string from the Intent and display it alongside the other user data:

// Inside onCreate() of your detail Activity
Intent intent = getIntent();
String name = intent.getStringExtra("USER_NAME");
String surname = intent.getStringExtra("USER_SURNAME");
String email = intent.getStringExtra("USER_EMAIL");
String gender = intent.getStringExtra("USER_GENDER");

// Update your TextView to show all data (example formatting)
userDetailsTextView.setText(String.format("%s %s %s %s", name, surname, email, gender));

Bonus: Capture Selection in Real-Time

If you want to update the gender value as soon as the user picks an option (instead of waiting until submission), attach a listener to the RadioGroup:

sexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton selectedBtn = findViewById(checkedId);
        if (selectedBtn != null) {
            selectedGender = selectedBtn.getText().toString();
        }
    }
});

This way, selectedGender will automatically update whenever the user switches between male and female.

That’s all you need! Now when you tap a ListView item, the gender text will be passed along and displayed exactly like the other user details.

内容的提问来源于stack exchange,提问作者Miguel Heredia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:25:54