java - 如何在Android中获取所选单选按钮的id

标签 java android radio-button

在我的项目中,我为用户创建了 3 个单选按钮用于多项选择选项。我在 SQLite 数据库中有一些问题和答案,我在 Activity 中检索这些问题和答案,然后将它们设置在 RadioGroup 中。到目前为止,它工作得很好,但现在当我想获取所选单选按钮的所有 ID,然后对其进行计数时。我正在使用 'com.stepstone.stepper:material-stepper:4.3.1' 库 ( Reference Link )。

我的 Activity

public class KlasifikasiActivity extends AppCompatActivity implements StepperLayout.StepperListener {

    @Override
    public void onCompleted(View completeButton) {
        Toast.makeText(this, "onCompleted!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(VerificationError verificationError) {
        Toast.makeText(this, "onError! -> " + verificationError.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStepSelected(int newStepPosition) {
        Toast.makeText(this, "onStepSelected! -> " + newStepPosition, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onReturn() {
        finish();
    }
}

我的StepFragment

public class StepFragmentExample extends Fragment implements Step {


    public StepFragmentExample() {
        // Required empty public constructor
    }

    RadioGroup mRadioGroup;
    RadioButton mRadioButton1;
    RadioButton mRadioButton2;
    RadioButton mRadioButton3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Bundle bundle = this.getArguments();
        Log.d("##", "onCreateView: " + bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) ;

        View view = inflater.inflate(R.layout.fragment_step_fragment_example, container, false);

        mRadioGroup = view.findViewById(R.id.radioGroupWrapper);

        DatabaseHelper mDBHelper = new DatabaseHelper(getContext());
        Log.d("#######", String.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)));
        final List<Klasifikasi> lk = mDBHelper.getListKlasifikasiByGroup(String.valueOf(Integer.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) + 1));
        final RadioButton[] rb = new RadioButton[lk.size()];
        Log.d("##sz", "onCreateView: " + lk.size()) ;


        int[][] states = new int[][] {
                new int[] { android.R.attr.state_enabled}, // enabled
                new int[] {-android.R.attr.state_enabled}, // disabled
                new int[] {-android.R.attr.state_checked}, // unchecked
                new int[] { android.R.attr.state_pressed}  // pressed
        };

        int[] colors = new int[] {
                Color.BLACK,
                Color.BLACK,
                Color.BLACK,
                Color.BLACK
        };

        mRadioButton1 = view.findViewById(R.id.checkbox1);
        mRadioButton2 = view.findViewById(R.id.checkbox2);
        mRadioButton3 = view.findViewById(R.id.checkbox3);

        if(lk.size() > 0) {
            Klasifikasi kl0 = lk.get(0);
            Klasifikasi kl1 = lk.get(1);
            Klasifikasi kl2 = lk.get(2);
            mRadioButton1.setText(kl0.getKlasifikasi());
            mRadioButton2.setText(kl1.getKlasifikasi());
            mRadioButton3.setText(kl2.getKlasifikasi());
        }

        return view;
    }

    @Nullable
    @Override
    public VerificationError verifyStep() {
        return null;
    }

    @Override
    public void onSelected() {

    }

    @Override
    public void onError(@NonNull VerificationError error) {

    }
}

如何获取所选单选按钮的 ID?我的完整代码在 this链接。

最佳答案

使用mRadioGroup.getCheckedRadioButtonId()获取RadioGroup内所选RadioButton的id

关于java - 如何在Android中获取所选单选按钮的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681382/

相关文章:

java - 使用 openssl 创建 .p12 信任库

java - 单击数字键盘上的 "Done"按钮时应用程序崩溃

android - Cordova 项目可以在 Android 和 iPad 上运行,但不能在 iPhone 上运行

javascript - Ionic - 默认选中 2 个单选按钮(不同的列表)

java - 使用公共(public)字段以外的 getter/setter 来实现二进制兼容性?

java - "private package"是什么意思?

java - Spark Java中计算中位数

java - Android 首选项屏幕布局

html - 制作单选按钮 "checkable": HTML

javascript - jquery 动态选择单选按钮(表单重置问题)