如何在Android中显示单选按钮的内容?

时间:2017-09-15 12:22:40

标签: android radio-button

我试图在textview中显示单选按钮的内容而不点击任何按钮。 我的活动是这样的: 在这里,我选择了radioButton之一然后我在editText中写了一些数字。例如,如果我选择第一个按钮并在editText中写入3。我应该在底部的textView中看到600(200 * 3)。

My activity

我试着编写函数,但它给出了这个错误:

09-15 12:19:48.418 15984-15984/com.example.merve.tev E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.merve.tev, PID: 15984
                                                                       java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                           at android.view.View.performClick(View.java:5637)
                                                                           at android.widget.CompoundButton.performClick(CompoundButton.java:122)
                                                                           at android.view.View$PerformClick.run(View.java:22429)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                           at android.view.View.performClick(View.java:5637) 
                                                                           at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                                                                           at android.view.View$PerformClick.run(View.java:22429) 
                                                                           at android.os.Handler.handleCallback(Handler.java:751) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                        Caused by: java.lang.NumberFormatException: For input string: ""
                                                                           at java.lang.Integer.parseInt(Integer.java:533)
                                                                           at java.lang.Integer.parseInt(Integer.java:556)
                                                                           at com.example.merve.tev.GenelBagis.onRadioButtonClicked(GenelBagis.java:32)
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                           at android.view.View.performClick(View.java:5637) 
                                                                           at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                                                                           at android.view.View$PerformClick.run(View.java:22429) 
                                                                           at android.os.Handler.handleCallback(Handler.java:751) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我的课程:

public class GenelBagis extends AppCompatActivity {

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

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        Integer amount = 0 ;
        Integer month = 0;
        boolean checked = ((RadioButton) view).isChecked();
        TextView textView7 = (TextView) findViewById(R.id.textView7);
        EditText EditText = (EditText) findViewById(R.id.bootstrapEditText);

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.meslekLisesi:
                if (checked)
                    month = Integer.parseInt( EditText.getText().toString() );;
                    amount = month * 200;
                    textView7.setText(amount);
                break;
            case R.id.YuksekOgrenim:
                if (checked)
                    month = Integer.parseInt( EditText.getText().toString() );;
                    amount = month * 510;
                    textView7.setText(amount);
                break;

            case R.id.YuksekLisans:
                if (checked)
                    month = Integer.parseInt( EditText.getText().toString() );;
                    amount = month * 850;
                    textView7.setText(amount);
                break;

            case R.id.Doktora:
                if (checked)
                    month = Integer.parseInt( EditText.getText().toString() );;
                    amount = month * 1150;
                    textView7.setText(amount);
                    break;



        }
    }
}

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.merve.tev.GenelBagis"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <com.beardedhen.androidbootstrap.BootstrapEditText
        android:layout_width="0dp"
        android:layout_height="25dp"
        app:bootstrapSize="md"
        app:bootstrapBrand="info"
        android:id="@+id/bootstrapEditText"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView4"
        android:layout_marginEnd="24dp"
        app:layout_constraintRight_toRightOf="@+id/textView3"
        android:layout_marginTop="38dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="36dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView3"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="24dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Kaç Aylık Burs Vermek İstersiniz?"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="16dp"
        android:layout_marginTop="44dp"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Burs Vermek İstediğiniz Toplam Tutar"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="126dp"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="124dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp"
        />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="116dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1">


        <RadioButton
            android:id="@+id/meslekLisesi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Meslek Lisesi (Aylık) 200 TL" />

        <RadioButton
            android:id="@+id/YuksekOgrenim"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Yüksek Öğrenim (Aylık) 510 TL" />

        <RadioButton
            android:id="@+id/YuksekLisans"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Yüksek Lisans (Aylık) 850 TL" />

        <RadioButton
            android:id="@+id/Doktora"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Doktora (Aylık) 1150 TL" />


    </RadioGroup>

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="69dp"
        app:layout_constraintTop_toBottomOf="@+id/bootstrapEditText"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="74dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

</android.support.constraint.ConstraintLayout>

如何在没有任何按钮的情况下在TextView中查看单选按钮值和EditText值的产品?

2 个答案:

答案 0 :(得分:1)

尝试onChekedChangeListener()

radioBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

答案 1 :(得分:1)

尝试为RadioButton Click事件执行此操作

 radio1.setOnCheckedChangeListener(this);
radio2.setOnCheckedChangeListener(this);
 radio3.setOnCheckedChangeListener(this);


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()){
        case R.id.radio1:
                if(isChecked){

                }
            break;
        case R.id.radio2:
            if(isChecked){

            }
            break;
        case R.id.radio3:
            if(isChecked){

            }
            break;
    }
}