【问题标题】:Getting and returning the text from a radio group从广播组获取和返回文本
【发布时间】:2019-10-03 11:04:25
【问题描述】:

我有一个广播组,可以选择“是”、“否”和“也许”。我想要一个返回用户选择的函数。

这是我的代码

 <RadioGroup
        android:id="@+id/answerRadioGroup"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/submitButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myTextView"
        app:layout_constraintVertical_bias="0.26">

        <RadioButton
            android:id="@+id/yesRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/noRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="No" />

        <RadioButton
            android:id="@+id/maybeRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Maybe" />

    </RadioGroup>

这是我正在尝试使用的功能:

private String checkGuess(){
        guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
        int selectedId = guessRadioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) findViewById(selectedId);
        String s = (String) radioButton.getText().toString();
        return s;
    }

当我用类似String s = "Whatever" 的东西替换String s = (String) radioButton.getText().toString(); 时,这个功能似乎起作用了。问题似乎在于使用getText() 方法。

这是我在 MainActivity.java 中用来显示文本的:

TextView tempTextView = (TextView) findViewById(R.id.tempTextView);
        tempTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String abc = checkGuess();
                tempTextView.setText("You chose: " + abc);
            }
        });

【问题讨论】:

  • 这很相似,我尝试过使用这种方式,但我不想将它放入 Toast 中。我想使用一个函数来返回我选择的项目的值,以便稍后进行比较。

标签: java android radio-button android-studio-3.0 android-radiogroup


【解决方案1】:

你应该为radioButton使用CustomListener

radioButton.setOnCheckedChangeListener(new CustomListener());
class CustomListener implements   CompoundButton.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()){
                case R.id.rd1:

                    break;
            }
    }
}

【讨论】:

    【解决方案2】:

    您应该使用您的 radioGroup 作为查找单选按钮的父级。而且在使用 toString() 时也不需要强制转换为字符串。

    private String checkGuess(){
        guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
        int selectedId = guessRadioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) guessRadioGroup.findViewById(selectedId);
        String s = radioButton.getText().toString();
        return s;
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2023-03-15
      • 2022-01-09
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多