LoginSignup
1
1

More than 5 years have passed since last update.

AndroidでRadioButtonを出す

Last updated at Posted at 2017-01-02
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.joji.mytest2.MainActivity">

    <RadioGroup
        android:id="@+id/rgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <RadioButton
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="windows"
        android:checked="true"
        />
    <RadioButton
        android:id="@+id/rg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Linux"
        />
    <RadioButton
        android:id="@+id/rg3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mac"
        />
    </RadioGroup>

</RelativeLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

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

        RadioGroup rgroup = (RadioGroup)findViewById(R.id.rgroup);
        rgroup.setOnCheckedChangeListener(
            new RadioGroup.OnCheckedChangeListener(){
                public void onCheckedChanged(RadioGroup group, int checkedId){
                    RadioButton radio = (RadioButton)group.findViewById(checkedId);
                    Toast.makeText(
                            MainActivity.this,
                            String.format("[%s]が選択されました", radio.getText()),
                            Toast.LENGTH_SHORT).show();
                };
            }
        );
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1