KotlinでAndroidアプリのradiobuttonを追加する方法を現役エンジニアが解説【初心者向け】
初心者向けにKotlinでAndroidアプリのradiobuttonを追加する方法について現役エンジニアが解説しています。radiobuttonとは複数の選択肢から1つだけを選択する際に使用するUI部品です。RadioGroupの中にradiobuttonを複数設定していく形で、選択された場合の処理方法も解説します。
テックアカデミーマガジンは受講者数No.1のプログラミングスクール「テックアカデミー」が運営。初心者向けにプロが解説した記事を公開中。現役エンジニアの方はこちらをご覧ください。 ※ アンケートモニター提供元:GMOリサーチ株式会社 調査期間:2021年8月12日~8月16日 調査対象:2020年8月以降にプログラミングスクールを受講した18~80歳の男女1,000名 調査手法:インターネット調査
KotlinでAndroidアプリのradiobuttonを追加する方法について、TechAcademyのメンター(現役エンジニア)が実際のコードを使用して初心者向けに解説します。
初心者向けにKotlinの入門向けサイトをまとめた記事もありますので読んでみてください。
なお本記事は、TechAcademyのオンラインブートキャンプ、Androidアプリ開発講座の内容をもとに作成しています。

今回は、Androidアプリ開発に関する内容だね!

どういう内容でしょうか?

KotlinでAndroidアプリのradiobuttonを追加する方法について詳しく説明していくね!

お願いします!
目次
radiobuttonとは
radiobuttonとは複数の選択肢から1つだけを選択する際に使用するUI部品です。

radiobuttonを配置してみよう
radiobuttonはRadioGroupの中にRadioButtonを配置する構成です。Android Developerにサンプルが紹介されていますので確認しながら進めましょう。
Android Developer – Radio Button
例えば「さくら、うめ、ぼたん」のRadio Buttonを配置するには以下のように記述します。
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="さくら"/>
<RadioButton android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="うめ"/>
<RadioButton android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="ぼたん"/>
</RadioGroup>
radiobuttonが選択された時の処理を追加してみよう
radiobuttonが選択された時の処理を追加するには、 レイアウトファイルで onClickイベントにイベントハンドラーを割り付けます。
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="さくら"/>
次にKotlinのコードでイベントハンドラーの内容を記載します。
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
// Is the button now checked?
val checked = view.isChecked
// Check which radio button was clicked
when (view.getId()) {
R.id.radio1 ->
if (checked) {
// 1つ目のRadioButtonが選択された時の処理
}
R.id.radio2 ->
if (checked) {
// 2つ目のRadioButtonが選択された時の処理
}
R.id.radio3 ->
if (checked) {
// 3つ目のRadioButtonが選択された時の処理
}
}
}
}
実際にサンプルを作成して確認してみましょう。
プロジェクトを「Empty Activity」で新規作成し、レイアウトファイルとKotlinのソースコードを以下のように変更します。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="さくら"/>
<RadioButton android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="うめ"/>
<RadioButton android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="ぼたん"/>
</RadioGroup>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text=""
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/group" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivityクラス
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
// Is the button now checked?
val checked = view.isChecked
var item = view.text.toString()
var msg = ""
// Check which radio button was clicked
when (view.getId()) {
R.id.radio1 ->
if (checked) {
msg = "桜"
}
R.id.radio2 ->
if (checked) {
msg = "梅"
}
R.id.radio3 ->
if (checked) {
msg = "牡丹"
}
}
textView.text = "「${msg}(${item})」が選択されました。"
}
}
}
実行結果は以下のとおりです。RadioButtonが選択されると TextViewにメッセージが表示されます。

まとめ
今回の記事ではRadioButtonの使い方について学習しました。
監修してくれたメンター
| 太田和樹(おおたかずき)
ITベンチャー企業のPM兼エンジニア。 普段は主に、Web系アプリケーション開発のプロジェクトマネージャーとプログラミング講師を行っている。守備範囲はフロントエンド、モバイル、サーバサイド、データサイエンティストと幅広い。その幅広い知見を生かして、複数の領域を組み合わせた新しい提案をするのが得意。 開発実績:画像認識技術を活用した駐車場混雑状況把握(実証実験)、音声認識を活用したヘルプデスク支援システム、Pepperを遠隔操作するアプリの開発、大規模基幹系システムの開発・導入マネジメント。 地方在住。仕事のほとんどをリモートオフィスで行う。通勤で消耗する代わりに趣味のDIYや家庭菜園、家族との時間を楽しんでいる。 |

内容分かりやすくて良かったです!

ゆかりちゃんも分からないことがあったら質問してね!

分かりました。ありがとうございます!
TechAcademyでは、初心者でも最短4週間で、Kotlinの技術を使ってAndroidアプリ開発を習得できる、オンラインブートキャンプを開催しています。
また、現役エンジニアから学べる無料体験も実施しているので、ぜひ参加してみてください。