如何在android中使用单选按钮进行数学运算

时间:2017-07-15 13:50:15

标签: java android onclicklistener buttonclick radio-group

[Android新手]将4个数学运算作为选项添加到辐射按钮时需要帮助。 另外,我需要在选择单选按钮选项时执行相应的选项。

package com.sivaneshsg.wallet;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
    int cashamount = 0;
    int cardamount = 0;
    int walletamount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText et = (EditText) findViewById(R.id.inputamount);
        final RadioButton card1 = (RadioButton) findViewById(R.id.card1);
        final RadioButton cash1 = (RadioButton) findViewById(R.id.cash1);
        final RadioButton card2 = (RadioButton) findViewById(R.id.card2);
        final RadioButton cash2 = (RadioButton) findViewById(R.id.cash2);
        final RadioGroup rg =(RadioGroup) findViewById(R.id.rgroup);
        final TextView t1 = (TextView) findViewById(R.id.amountcard);
        final TextView t2 = (TextView) findViewById(R.id.amountcash);
        final TextView t3 = (TextView) findViewById(R.id.amountwallet);
        Button but = (Button) findViewById(R.id.button);
        final int amount = Integer.parseInt(et.getText().toString());

        cash1.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
            cash2.setChecked(false);
            card1.setChecked(false);
            card2.setChecked(false);
            cashamount = cashamount + amount;
        }
    });

        card1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            cash2.setChecked(false);
            cash1.setChecked(false);
            card2.setChecked(false);
            cardamount=cardamount+amount;
        }
    });

        cash2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            cash1.setChecked(false);
            card1.setChecked(false);
            card2.setChecked(false);
            cashamount = cashamount - amount;
        }
    });

        card2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cash2.setChecked(false);
                cash1.setChecked(false);
                card2.setChecked(false);
                cardamount=cardamount-amount;
            }
        });

        but.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                t1.setText("Amount in Card : RS. " + cardamount);
        t2.setText("Amount in Cash : Rs. " + cashamount);
        walletamount = cardamount + cashamount;
        t3.setText("Total Amount in Wallet : RS. " + walletamount);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您的金额变量在onCreate()方法中取值,该方法最初为0