如何在Java上运行Android Radio Group

问题描述 投票:0回答:1

我想在 Android studio 上使用带有按钮的单选组

我正在开发一个 Android 应用程序,我需要在 AlertDialog 中使用 RadioGroup 捕获用户输入。下面是我正在使用的代码片段:

import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    Button bt;
    RadioGroup g1;
    RadioGroup g2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt = findViewById(R.id.button);
        g1 = findViewById(R.id.radioGroup);
        g2 = findViewById(R.id.radioGroup2);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                alertDialogBuilder.setTitle("Alert");
                alertDialogBuilder.setMessage("Click Yes to Submit").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int ans1 = g1.getCheckedRadioButtonId();
                        int ans2 = g2.getCheckedRadioButtonId();

                        String selectedOptionQ1 = "";
                        String selectedOptionQ2 = "";

                        if(ans1 != -1) {
                            View selectedRadioButtonViewQ1 = g1.findViewById(ans1);
                            if(selectedRadioButtonViewQ1 != null && selectedRadioButtonViewQ1 instanceof RadioButton) {
                                selectedOptionQ1 = ((RadioButton) selectedRadioButtonViewQ1).getText().toString();
                            }
                        }

                        if(ans2 != -1) {
                            View selectedRadioButtonViewQ2 = g2.findViewById(ans2);
                            if(selectedRadioButtonViewQ2 != null && selectedRadioButtonViewQ2 instanceof RadioButton) {
                                selectedOptionQ2 = ((RadioButton) selectedRadioButtonViewQ2).getText().toString();
                            }
                        }

                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra("selectedOptionQ1", selectedOptionQ1);
                        intent.putExtra("selectedOptionQ2", selectedOptionQ2);
                        startActivity(intent);
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }
}

在此代码中,我有一个 AlertDialog,其中包含两个 RadioGroup 实例(g1 和 g2),每个实例包含多个 RadioButton 选项。单击对话框中的“是”按钮后,我想从两个 RadioGroup 中捕获选定的选项,并使用意图将它们传递给另一个活动。

代码似乎工作正常,但我想知道是否有更有效或更简洁的方法来处理 Android 中 RadioGroup 中单选按钮的选择。我是否应该考虑实现此功能的最佳实践或替代方法?任何见解或建议将不胜感激!

java android android-intent android-radiogroup
1个回答
0
投票

主要活动2

package com.example.lab4q1;


import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

tv = (TextView)findViewById(R.id.textViewRes);

String selectedOptionQ1 = getIntent().getStringExtra("selectedOptionQ1");
String selectedOptionQ2 = getIntent().getStringExtra("selectedOptionQ2");

int s=0;
if("Option 1".equals(selectedOptionQ1))
{
s = s+1;
}
if("Option 2".equals(selectedOptionQ2))
{
s = s+1;
}

tv.setText(String.valueOf(s));
// tv.setText(selectedOptionQ2);
}
}

© www.soinside.com 2019 - 2024. All rights reserved.