There is a display:

enter image description here

And the code :

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ ImageView plus; ImageView minus; TextView tvResult; TextView cena; TextView cena_53_skritaya; TextView cena_73_skritaya; RadioGroup radioGroup_cena; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); plus = (ImageView) findViewById(R.id.plus); plus.setOnClickListener(this); minus= (ImageView) findViewById(R.id.minus); minus.setOnClickListener(this); tvResult = (TextView) findViewById(R.id.tvResult); cena = (TextView) findViewById(R.id.cena); cena_53_skritaya = (TextView) findViewById(R.id.textView_53); cena_73_skritaya = (TextView) findViewById(R.id.textView_73); radioGroup_cena = (RadioGroup) findViewById(R.id.radioGroup_cena); radioGroup_cena.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton_250: cena.setText("53"); break; case R.id.radioButton_500: cena.setText("73"); break; } } }); } @Override public void onClick(View v) { int parse_kolichestvo; int parse_cena_53; int num2 = 1; int result = 0; int resultcena=0; // читаем TextView и заполняем переменные числами parse_kolichestvo = Integer.parseInt(tvResult.getText().toString()); parse_cena_53 = Integer.parseInt(cena_53_skritaya.getText().toString()); switch (v.getId()) { case R.id.plus: result = parse_kolichestvo + num2; resultcena= result*parse_cena_53; } switch (v.getId()) { case R.id.minus: result = parse_kolichestvo - num2; if (parse_kolichestvo==1){ return; } resultcena= result*parse_cena_53; } // формируем строку вывода tvResult.setText( String.valueOf(result) ); cena.setText( String.valueOf(resultcena) ); } } 

As you can see in OnClick, the program parsit the first number (53) and multiplies by the quantity, in this case 1, the plus and minus (Image View) changes the quantity and sets the step. Now the question is how to transfer this cycle to

  switch (checkedId) { case R.id.radioButton_250: cena.setText("53"); break; 

And accordingly set another cycle for working with

  case R.id.radioButton_500: cena.setText("73"); break; 

But with parsing already 73, not 53. I tried myself, but there are problems with the designation ImageView for each of the cycles.

enter image description here

    1 answer 1

    It’s wisely like that ... I didn’t see a single loop in the attached code that you want to transfer ... But looking at your switch case structure in the onClick method, I realized that you don’t really know what you are doing :). However, we all were once so - so I still try.

    In general, from what I tried to solve from your rebus (I am not sure that I correctly solved it), you need to use data from the field in the onClick method, which are filled in by means of cell selection in CheckGroup , and not from Integer.parseInt(cena_53_skritaya.getText().toString()) . Then it will not matter to you which element is selected and the logic will correctly work out precisely for the price that you entered in the CheckGroup .

    • Thank you), That's right, you decided the rebus))) - Romik romikromik
    • @ Eugene Troyanskiyy I do not want to open a new topic, but you will not tell me how to correctly access the variable in onClick from OnCheckChanged. I updated the body of the letter in screenshots of the development environment, so that you understand what I mean .) - Romik romikromik
    • @Romikromikromik If you have another problem - create a new question, here Q & A, and not a forum with discussions. The principle of this - one specific question - the right answer, without any - "but I also have .." or the like. - pavlofff
    • understood thanks. - Romik romikromik
    • @Romikromikromik create a global variable and assign a value to it in each onCheckChange case. And in onClick just use this global variable in which exactly the price that is set at the moment will lie. - Eugene Troyanskii