简体   繁体   English

如何在Android的radiogroup内部使用edittext?

[英]how to use edittext inside the radiogroup in android?

**I have used four radio buttons for adding and subtraction the amount in the wallet. **我使用了四个单选按钮来添加和减去钱包中的金额。 whenever i open the app it crashes .Whenever i open the app it crashes i don't what is error please someone help me. 每当我打开应用程序时,它就会崩溃。每当我打开应用程序时,它就会崩溃,我不是什么错误,请有人帮我。 i am new to android. 我是android新手。 In this app i have used four radio buttons for add the amount and subtracting the amount. 在这个应用程序中,我使用了四个单选按钮来增加和减少金额。 ** **

<?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. ** 
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);

        }
    });
}

} }

try the above code your problem was here 试试上面的代码你的问题在这里

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

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

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