-1

以下は私の activity_main.xml コードです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical">


       <RadioGroup
           android:layout_width="match_parent"
           android:layout_height="300dp"
           android:id="@+id/rgr">

           <RadioButton
               android:id="@+id/rb1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:text="Mechanical" />

           <RadioButton
               android:id="@+id/rb2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:text="Electrical" />

           <RadioButton
               android:id="@+id/rb3"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:text="Civil" />

           <RadioButton
               android:id="@+id/rb4"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:text="Computer Science" />
       </RadioGroup>


</LinearLayout>

以下は MainActivity.java コードです

import android.support.v7.app.AppCompatActivity;                                                        
import android.os.Bundle;                                                                               
import android.view.View;                                                                               
import android.widget.RadioButton;                                                                      
import android.widget.RadioGroup;                                                                       
import android.widget.Toast;                                                                            

public class MainActivity extends AppCompatActivity                                                     
{           // https://www.youtube.com/watch?v=n7c3bIWcgZo                                              



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

        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgr);                                     
        RadioButton radioButton1 = (RadioButton)findViewById(R.id.rb1);                                 
        RadioButton radioButton2 = (RadioButton)findViewById(R.id.rb2);                                 
        RadioButton radioButton3 = (RadioButton)findViewById(R.id.rb3);                                 
        RadioButton radioButton4 = (RadioButton)findViewById(R.id.rb4);                                 

        if(radioButton1.isChecked())                                                                    
        {                                                                                               
               Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
        }                                                                                               
        else if(radioButton2.isChecked())                                                               
        {                                                                                               
               Toast.makeText(getApplicationContext(), radioButton2.getText(),Toast.LENGTH_LONG).show();
        }                                                                                               
        else if(radioButton3.isChecked())                                                               
        {                                                                                               
               Toast.makeText(getApplicationContext(), radioButton3.getText(),Toast.LENGTH_LONG).show();
        }                                                                                               
        else if(radioButton4.isChecked())                                                               
        {                                                                                               
               Toast.makeText(getApplicationContext(), radioButton4.getText(),Toast.LENGTH_LONG).show();
        }                                                                                               
    } 

今、アプリを実行すると、ラジオ ボタンが選択されますが、特定のボタンを選択してもトースト メッセージが表示されません。

また、ラジオグループを削除してラジオボタンのみを保持しようとしましたが、それでも isChecked() 機能が実行されていないと思います。

この問題を解決するために私を助けてください。

4

2 に答える 2