【问题标题】:How to use radiobuttons with array?如何使用带有数组的单选按钮?
【发布时间】:2017-12-25 03:19:14
【问题描述】:

我有兴趣制作一款适用于 Android 的 APP,其中包含 30 个个性档案问题,每个问题都有两种可供选择的选项。我考虑过使用 RadioButton,但由于有 30 个问题,我不想一次将它们全部显示在屏幕上,我只想显示一个带有两个备选方案的问题,并且每个备选方案之一的选择都已称为另一个问题.

是否可以在不创建 30 个活动的情况下做到这一点?

我看到可以做数组,但我不知道如何从一个问题运行到另一个问题。

非常感谢!!!

【问题讨论】:

  • 你为什么复制自己两次?
  • 我认为创建 30 个活动不好,为什么不一次只显示 1 个问题并在用户选择正确答案后更改问题,这样会更有效。

标签: java android arrays radio-button


【解决方案1】:

此代码实现了所需的功能。这是非常基本的,可以根据您的需要进行改进。只需将此活动和布局添加到您的项目中即可。

Java 文件:此代码使用单一布局并重新创建单选按钮单击下一个问题的视图

    public class QuestionsActivity extends AppCompatActivity implements 
    RadioGroup.OnCheckedChangeListener{
    LinkedHashMap<String,RadioGroup> questionList;
    LinkedHashMap<String,String> answerList;
    ArrayList<String> keys=new ArrayList<>();
    int keyCounter=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        questionList=new LinkedHashMap<>(); //This Map contains all the questions with two radio button (options)
        answerList=new LinkedHashMap<>(); //This Map will contain question along with selected answer.

        initQuestions(); //This method will add 30 questions with options

        keys.addAll(questionList.keySet());

        showQuestions(keys.get(keyCounter));//This method will show the first question






    }

    private void showQuestions(String key) {
        TextView textView=(TextView)findViewById(R.id.tv_question);
        textView.setText(key);
        LinearLayout layout =(LinearLayout)findViewById(R.id.questionsLayout);
        layout.removeAllViews();
        RadioGroup rg=questionList.get(key);
        rg.setOrientation(LinearLayout.HORIZONTAL);
        layout.addView(rg);
        rg.setOnCheckedChangeListener(this);


    }

    private void initQuestions() {
        for (int i = 1; i <=3; i++) {
            RadioGroup rg=new RadioGroup(this);
            RadioButton rb1 =new RadioButton(this);
            rb1.setText("Q "+i+" RadioButton 1");
            RadioButton rb2 =new RadioButton(this);
            rb2.setText("Q "+i+" RadioButton 2");
            rg.addView(rb1);rg.addView(rb2);
            questionList.put("Question "+i,rg);


        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        RadioButton rb=(RadioButton) findViewById(group.getCheckedRadioButtonId());




        if(keyCounter<questionList.size()) {
            answerList.put(keys.get(keyCounter),rb.getText().toString());  // Putting the question and selected answer to 'answerList' map.
            keyCounter++;
            if(keyCounter<questionList.size()) {
                showQuestions(keys.get(keyCounter));// showing the next question.
            }

        }else {
            Toast.makeText(this, "You've answered all the questions.", Toast.LENGTH_SHORT).show();
        }

        for (String s : answerList.keySet()) {
            System.out.println("Q--> "+s+", A--> "+answerList.get(s)); // Here you can see all the questions and selected answers on your logs(AndroidMonitor).
        }
    }
    }

布局文件:该文件包含一个用于显示问题的 textView 和一个包含 RadioGroup 的线性布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="edios.endlessscrollrecycler.QuestionsActivity">

        <TextView
            android:id="@+id/tv_question"
            android:layout_marginTop="10dp"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/questionsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></LinearLayout>


</LinearLayout>

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2014-04-27
    • 1970-01-01
    • 2015-05-20
    • 2021-07-03
    • 2019-11-02
    相关资源
    最近更新 更多