方法②:匿名クラスを用いない方法
package com.example.radiobuttonsample2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Kyo kyo = new Kyo();
Wee wee = new Wee();
RadioGroup rg1 = (RadioGroup) findViewById(R.id.RadioGroupKamoku);
rg1.setOnCheckedChangeListener(kyo);
RadioGroup rg2 = (RadioGroup) findViewById(R.id.RadioGroupWeekly);
rg2.setOnCheckedChangeListener(wee);
}
class Kyo implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb1 = (RadioButton) findViewById(checkedId);
if (rb1.isChecked() == true) {
Toast.makeText(
MainActivity.this,
((RadioButton) findViewById(checkedId)).getText()
+ "を選択しました。", Toast.LENGTH_SHORT).show();
}
}
}
class Wee implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb2 = (RadioButton) findViewById(checkedId);
if (rb2.isChecked() == true) {
Toast.makeText(
MainActivity.this,
((RadioButton) findViewById(checkedId)).getText()
+ "を選択しました。", Toast.LENGTH_SHORT).show();
}
}
}
}
activity_main.xml
今回の画面の定義(activity_main.xml)は下記になります。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<RadioGroup
android:id="@+id/RadioGroupKamoku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Android" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioButton1"
android:text="java" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioButton2"
android:text="LAMP" />
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RadioGroupKamoku"
android:padding="@dimen/padding_medium"
android:gravity="center"
android:text="曜日 " />
<RadioGroup
android:id="@+id/RadioGroupWeekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2">
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:text="月曜日" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioButton4"
android:text="火曜日" />
<RadioButton
android:id="@+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioButton5"
android:text="水曜日" />
</RadioGroup>
</RelativeLayout>
これでOKです。