如何在android中的radiogroup里面使用edittext?

时间:2017-07-16 10:08:20

标签: java android android-edittext radio-button switch-statement

**我使用了四个单选按钮来添加和减去钱包中的金额。每当我打开应用程序时,它都会崩溃。无论什么时候我打开应用程序它都会崩溃我不知道什么是错误,请有人帮助我。我是android的新手。在这个应用程序中,我使用了四个单选按钮来添加金额并减去金额。 **

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/form"
    tools:context="com.sivaneshsg.wallet.MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Amount"
        android:textSize="25sp"
        android:textStyle="italic"
        android:textColor="@android:color/black"
        android:paddingTop="5dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"/>
    <EditText
        android:id="@+id/inputamount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Amount in Rs."
        android:paddingTop="5dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:inputType="number"
        android:ems="10"
        />
    <RadioGroup
        android:id="@+id/rgroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"

        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Income"
        android:textStyle="italic"
            android:textSize="20sp"
        android:textColor="@android:color/black"
            android:paddingBottom="10dp"/>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    <RadioButton
                android:id="@+id/cash1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cash"
                android:textSize="15dp"
                android:paddingRight="10dp"

        android:textColor="@android:color/black"
                />
    <RadioButton
                android:id="@+id/card1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Card"

        android:textColor="@android:color/black"
                />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Expense"
            android:textSize="20dp"
            android:textStyle="italic"
            android:textColor="@android:color/black"
            android:paddingBottom="10dp"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/cash2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cash"
                android:textColor="@android:color/black"
                android:textSize="15sp"

                android:paddingRight="10dp"
                />

    <RadioButton
        android:id="@+id/card2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Card"
        android:textSize="15sp"

        android:textColor="@android:color/black"
        />
        </LinearLayout>
        </RadioGroup>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"

        />


    <TextView
        android:id="@+id/amountcard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Amount in Card : RS. 0"
        android:textSize="25sp"
        android:padding="10dp"/>
    <TextView
        android:id="@+id/amountcash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amount in Cash : Rs. 0"
        android:textSize="25sp"
        android:textColor="@android:color/black"
        android:padding="10dp"/>
    <TextView
        android:id="@+id/amountwallet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total Amount in Wallet : RS. 0"
        android:textSize="23sp"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:padding="10dp"/>

</LinearLayout> 









 package com.sivaneshsg.wallet;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public class MainActivity extends AppCompatActivity {

    int cashamount = 0;
    int cardamount = 0;
    int totalamount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        EditText et = (EditText) findViewById(R.id.inputamount);
        final int amount = Integer.parseInt(et.getText().toString());
        RadioGroup rg = (RadioGroup) findViewById(R.id.rgroup);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.card1) {
                    cardamount = cardamount + amount;
                } else if (checkedId == R.id.cash1) {
                    cashamount = cashamount + amount;
                } else if (checkedId == R.id.cash2) {
                    cashamount = cashamount - amount;
                } else if (checkedId == R.id.card2) {
                    cardamount = cardamount - amount;
                }

            }
        });
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView cash = (TextView) findViewById(R.id.amountcash);
                TextView card = (TextView) findViewById(R.id.amountcard);
                TextView wallet = (TextView) findViewById(R.id.amountwallet);
                cash.setText("Amount in Cash : RS. " + cashamount);
                card.setText("Amount in Card : RS. " + cardamount);
                totalamount = cashamount + cashamount;
                wallet.setText("Total Amount in Wallet : RS. " + totalamount);

            }
        });
    }
}

**Whenever i open the app it crashes i don't what is error please someone help me. i am new to android. In this app i have used four radio buttons for add the amount and subtracting the amount. ** 

1 个答案:

答案 0 :(得分:0)

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;


public class teste extends AppCompatActivity {

int cashamount = 0;
int cardamount = 0;
int totalamount;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.iuiu);


    EditText et = (EditText) findViewById(R.id.inputamount);
    final String amount = (et.getText().toString());
    RadioGroup rg = (RadioGroup) findViewById(R.id.rgroup);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.card1) {
                cardamount = Integer.parseInt(cardamount + amount);
            } else if (checkedId == R.id.cash1) {
                cashamount = Integer.parseInt(cashamount + amount);
            } else if (checkedId == R.id.cash2) {
                cashamount -= Integer.parseInt(amount);
            } else if (checkedId == R.id.card2) {
              cardamount -= Integer.parseInt(amount);
            }

        }
    });
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView cash = (TextView) findViewById(R.id.amountcash);
            TextView card = (TextView) findViewById(R.id.amountcard);
            TextView wallet = (TextView) findViewById(R.id.amountwallet);
            cash.setText("Amount in Cash : RS. " + cashamount);
            card.setText("Amount in Card : RS. " + cardamount);
            totalamount = cashamount + cashamount;
            wallet.setText("Total Amount in Wallet : RS. " + totalamount);

        }
    });
}

}

尝试以上代码,问题就在这里

 final int amount = Integer.parseInt(et.getText().toString());