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

Posted

技术标签:

【中文标题】如何在 Android 中显示单选按钮的内容?【英文标题】:How can I display the content of radio button in Android? 【发布时间】:2018-02-24 15:24:13 【问题描述】:

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

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

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_
    android:layout_
    tools:context="com.example.merve.tev.GenelBagis"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <com.beardedhen.androidbootstrap.BootstrapEditText
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
            android:layout_
            android:onClick="onRadioButtonClicked"
            android:text="Meslek Lisesi (Aylık) 200 TL" />

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

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

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


    </RadioGroup>

    <TextView
        android:id="@+id/textView7"
        android:layout_
        android:layout_
        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中看到radio Button值和EditText值的乘积?

【问题讨论】:

添加完整的堆栈跟踪和更多代码 month = Integer.parseInt( EditText.getText().toString() ); 试试month = Integer.parseInt( EditText.getText().toString()replaceAll("[^0-9]", "") ); 【参考方案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;
    

【讨论】:

【参考方案2】:

试试onChekedChangeListener()

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

            
        );

【讨论】:

以上是关于如何在 Android 中显示单选按钮的内容?的主要内容,如果未能解决你的问题,请参考以下文章

如何创建自定义 Android 单选按钮?

Android从选定的单选按钮获取价值

如何实现android中三个单选按钮横向排列且只能选一个?

如何检查单选按钮是不是在 Android 的单选组中被选中?

将单选按钮数据发送到下一个活动

如何在 Android 中验证单选按钮组?