繁体   English   中英

如何在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);
            }
        });
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM