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

How to use radio buttons for math operation in android

[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);
            }
        });
    }
}

您的 amount 变量在 onCreate() 方法中取值,初始值为 0