radiobuttons(将条件更改为onclick)

时间:2016-07-08 15:13:06

标签: java android

一般都有这个屏幕:

enter image description here

如何将条件从OnClick放到onCheckedChanged(radiobutton)。它仅适用于OnClick中的一个变量price_53_off,但我想在检查时,其他radiobutton(500 gr。)程序解析其他变量(price_73_off)并计算其他周期。

以下一些代码。

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
  ImageView plus;
  ImageView minus;
  TextView tvResult;
  TextView price;
  TextView price_53_off;
  TextView price_73_off;
  RadioGroup radioGroup_price;

  @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);
    price = (TextView) findViewById(R.id.price);
    price_53_off = (TextView) findViewById(R.id.price_53_off);
    price_73_off = (TextView) findViewById(R.id.price_73_off);
    radioGroup_price = (RadioGroup) findViewById(R.id.radioGroup_price);

    radioGroup_price.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {

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

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

  @Override
  public void onClick(View v) {
    int parse_quant;
    int parse_price_53;
    int num2 = 1;
    int result = 0;
    int resultprice=0;
    parse_quant = Integer.parseInt(tvResult.getText().toString());
    parse_price_53 = Integer.parseInt(price_53_off.getText().toString());

    switch (v.getId()) {
      case R.id.plus:
        result = parse_quant + num2;
        resultcena= result*parse_price_53;
    }

    switch (v.getId()) {
      case R.id.minus:
        result = parse_quant - num2;
        if (parse_quant==1){
          return;
        }
        resultprice= result*parse_price_53;
    }

    tvResult.setText( String.valueOf(result) );
    price.setText( String.valueOf(resultprice) );

  }
}

1 个答案:

答案 0 :(得分:0)

为了resultresultPrice变量可用onCheckChangedonClick,您需要将它们存储在类级而不是{ {1}}方法。