@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); radioGroup= (RadioGroup) findViewById(R.id.radiogroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected RadioButton radioButton_250=(RadioButton)findViewById(checkedId); textView.setText(radioButton_250.getText()); RadioButton radioButton_500=(RadioButton)findViewById(checkedId); textView.setText(radioButton_500.setText("70")); } }); } 

The first button that draws text works fine, but the second textView.setText(radioButton_500.setText("70")); Do not let me, how can I quickly fix it?

  • and what should be the result? - Android Android
  • the second button should just set the value, in this case 70 - Romik romikromik

1 answer 1

Line

textView.setText (radioButton_500.setText ("70"));

Should not compile because The setText () method requires a String as an argument, and the result of a call to radioButton_500.setText("70") returns void .

If you need to change the text in radioButton and then assign it somewhere, then take turns:

 radioButton_500.setText("70"); textView.setText(radioButton_500.getText()); 

To change the test by clicking on RadioButtom in RadioGroup, you need to do something like this:

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // checkedId is the RadioButton selected //RadioButton radioButton = (RadioButton) group.findViewById(checkedId); //textView.setText(radioButton.getText()); switch(checkedId) { case R.id.someID: //тут нужный текст установите break; case R.id.anotherID: //тут нужный текст установите break; } } }); 
  • The trick is that the text of the radiobutton itself should not be set but only in the textview, you say the string value as an argument, textView.setText (radioButton_500.setText (R.string.r_500)); is it like this?) - Romik romikromik
  • 2
    @Romikromikromik so maybe you need textView.setText ("70")? Just you write absurd things - Android Android
  • @AndroidAndroid. Well, to you, as a pro, they seem absurd, and I, as a beginner, of course know little semantics. If you do as you say, this is RadioButton radioButton_250 = (RadioButton) findViewById (checkedId); textView.setText ("69"); RadioButton radioButton_500 = (RadioButton) findViewById (checkedId); textView.setText ("80"); , then only the last value (80) is set in the textview, and it does not change. Since the variable radiobutton_500 and radiobutton_250 is not specified in textview.setText **** - Romik romikromik
  • @Romikromikromik, i.e. You need to click on one of the RadioButton to change the text in the TextView? ... - YuriiSPb
  • @Yuriy SPb yes, that’s what - Romik romikromik