質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

2447閲覧

Android Studio RadioButtonの条件分岐について

yasuun

総合スコア7

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2021/02/13 00:41

編集2021/02/13 02:47

前提・実現したいこと

RadioButton男、RadioButton女、どちらか選択し、男、女でそれぞれ異なる計算処理を戻したい。

発生している問題・エラーメッセージ

無し

該当のソースコード

class MainActivity : AppCompatActivity() { //下記3つはEditText型 var editWeight = binding.weight var editAge = binding.age var editHight = binding.hight //下記2つはRadioButton型 val radioMan = binding.man val radioWoma = binding.woman //下記3つはButton型 val infoButton = binding.infoBtn val keisanButton = binding.keisanButton val nextButton = binding.nextBtn private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) binding.welcomDA.setText("WelcomDaietAdviser") binding.textView2.setText("まずは基礎代謝を計算しましょう!") } //もしmanが選択されたのあればkisotaisyManの計算結果をkisotaisyaTextviewに,womanであればkisotaisyaWomanをtextViewに //val kisotaisyaMan = 13.397 * weight + 4.799 * hight - 5.677 * age + 88.362 //val kisotaisyaWoman = 9.247 * weight + 3.098 * hight - 4.33 * age + 447.593 fun onClickCalcButton(view: View) { val weight2 = editWeight.text.toString().toFloat() val age2 = editAge.text.toString().toFloat() val hight2 = editHight.text.toString().toFloat() if (view is RadioButton) { val checked = view.isChecked var kisotaisyaMan: Float = 0f var kisotaisyaWoman: Float = 0f when(view.getId()){ //viewのidを取得 → もしこのview.getIdがmanなら〜、womannara〜 R.id.man -> if (checked){ kisotaisyaMan=(13.397*weight2+4.799*hight2-5.677*age2+88.362).toFloat() } R.id.woman -> if (checked){ kisotaisyaWoman=(9.247 * weight2 + 3.098 * hight2 - 4.33 * age2 + 447.593).toFloat() } } } val weight = editWeight.text.toString().toFloatOrNull() val age = editAge.text.toString().toFloatOrNull() val hight = editHight.text.toString().toFloatOrNull() //3つの内どれかがnullであればダイアログを表示させる処理 if (weight == null || age == null || hight == null) { AlertDialog.Builder(this) .setTitle(R.string.dialog_title_invalid_input).setPositiveButton(android.R.string.ok, null).show() return } val data: SharedPreferences = getSharedPreferences("DATA", Context.MODE_PRIVATE) val editor = data.edit() editor.putFloat("WEIGHT", weight) editor.putFloat("AGE", age) editor.putFloat("HIGHT", hight) editor.apply() startActivity(Intent(this, ResultActivity1::class.java).apply { putExtra("WEIGHT", weight) putExtra("AGE", age) putExtra("HIGHT", hight) }) } } 【activityMain.xml】 <TextView android:id="@+id/welcomDA" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WelcomDietAdviser" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.060000002" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="まずは基礎代謝を計算しましょう!" android:textAppearance="@style/TextAppearance.AppCompat.Large" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/welcomDA" app:layout_constraintVertical_bias="0.07999998" /> <EditText android:id="@+id/weight" android:textSize="20sp" android:layout_width="70dp" android:layout_height="wrap_content" android:ems="10" android:hint="50kg" android:inputType="numberDecimal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" app:layout_constraintVertical_bias="0.120000005" /> <EditText android:id="@+id/age" android:layout_width="70dp" android:layout_height="wrap_content" android:ems="10" android:hint="30歳" android:inputType="numberDecimal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/weight" app:layout_constraintVertical_bias="0.120000005" /> <EditText android:id="@+id/hight" android:layout_width="70dp" android:layout_height="wrap_content" android:ems="10" android:hint="170cm" android:inputType="numberDecimal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/age" app:layout_constraintVertical_bias="0.120000005" /> <TextView android:id="@+id/kisotaisya" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="基礎代謝量" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/hight" app:layout_constraintVertical_bias="0.29000002" /> <Button android:id="@+id/nextBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/black" android:text="NEXT" android:textColor="@color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.77" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/kisotaisya" app:layout_constraintVertical_bias="0.8" /> <RadioGroup android:id="@+id/radioGroup" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/kisotaisya" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/hight"> <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:onClick="onClickCalcButton" app:layout_constraintBottom_toTopOf="@+id/kisotaisya" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.37" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/hight" /> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:onClick="onClickCalcButton" app:layout_constraintBottom_toBottomOf="@+id/man" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.65" app:layout_constraintStart_toStartOf="parent" /> </RadioGroup> <Button android:id="@+id/infoBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="情報を記録する" app:layout_constraintBottom_toTopOf="@+id/nextBtn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/kisotaisya" /> <Button android:id="@+id/keisanButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="計算する" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/nextBtn" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/kisotaisya" app:layout_constraintVertical_bias="0.79" />›4 ![イメージ説明](3340ff6cea762c3d4269dcb2c1ea937f.png)

試したこと

whenでの条件分岐を行いましたが、サイトを参考にしたため、いまいち理解ができておりません。
onClickCalcButtonメソッドの処理内容の理解として、
①editWeight.text.toString.toFloat()でフロート型に変換している。(age, hightも同じ処理)
②if文にて、もしviewがRadioButtonなら、When文にて引数viewのIDを参照して、manというIDなら、if(checked)←これはなんでしょうか?、kisotaisyaManの計算を行う、womanならkisotaisyawomanの計算を行う

流れとして、
身長、体重、年齢のEditTextにユーザー入力してもらった後、ラジオボタンで男か女か選び、その後にてtxtViewに計算結果を映し出すようにしたいです。
【下記は男女別での計算方法】
//val kisotaisyaMan = 13.397 * weight + 4.799 * hight - 5.677 * age + 88.362
//val kisotaisyaWoman = 9.247 * weight + 3.098 * hight - 4.33 * age + 447.593

補足情報(FW/ツールのバージョンなど)

初心者で就活用のポートフォリオを作成していて、何時間考えてもわからずお力をお借りしたいです。。。
助長な文で申し訳ございません。

またラジオボタンの選択処理と、男女別で計算を行う処理を分けた方がいいというアドバイスを頂き、何回も試しましたがどのようにすればいいかわかりませんでした。。

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/02/13 01:08

activity_main.xml も載せてください。
yasuun

2021/02/13 02:39

かしこまりました! よろしくお願い致します。
hoshi-takanori

2021/02/13 04:14

ありがとうございます。これ、起動しないですよね。 就活用のポートフォリオ、他人が手伝っていいのだろうか…。
guest

回答2

0

ベストアンサー

出血大サービスです。
動作を追いかけて自分でよく考えてみてください。

まず、先の回答者が指摘するとおり、binding が設定されるのは、onCreate()の中ですから、onCreate()が呼び出されるより前に、editWeight = binding.weight などとすることはできません。
null参照となり、アプリがコケます。

次に、肝心な質問の部分ですが、

・ラジオボタンが押されたときの処理 ・・・ fun onClickRadioButton(view: View)
男女どちらが選択されたかを man_is_checked, woman_is_checked に保存

・計算ボタンが押されたときの処理 ・・・ fun onClickCalcButton(view: View)
man_is_checked, woman_is_checked をみて、基礎代謝を計算し、結果表示

・記録ボタンが押されたときの処理 ・・・ fun onClickSaveButton(view: View)

の3つのイベントを分けて考えます。それぞれの処理をよく追いかけてください。

kotlin

1package ... 2import ... 3 4class MainActivity : AppCompatActivity() { 5 6 //下記3つはEditText型 7 lateinit var editWeight: EditText 8 lateinit var editAge: EditText 9 lateinit var editHight: EditText 10 11 //下記2つはRadioButton型 12 lateinit var radioMan: RadioButton 13 lateinit var radioWoma: RadioButton 14 15 //下記3つはButton型 16 lateinit var infoButton: Button 17 lateinit var keisanButton: Button 18 lateinit var nextButton: Button 19 20 // 男女どちらがチェックされたか 21 var man_is_checked = false 22 var woman_is_checked = false 23 24 var weight: Float? = 0f 25 var age: Float? = 0f 26 var height: Float? = 0f 27 28 private lateinit var binding: ActivityMainBinding 29 30 override fun onCreate(savedInstanceState: Bundle?) { 31 super.onCreate(savedInstanceState) 32 setContentView(R.layout.activity_main) 33 binding = ActivityMainBinding.inflate(layoutInflater) 34 setContentView(binding.root) 35 36 //下記3つはEditText型 37 editWeight = binding.weight 38 editAge = binding.age 39 editHight = binding.hight 40 41 //下記2つはRadioButton型 42 radioMan = binding.man 43 radioWoma = binding.woman 44 45 //下記3つはButton型 46 infoButton = binding.infoBtn 47 keisanButton = binding.keisanButton 48 nextButton = binding.nextBtn 49 50 binding.welcomDA.setText("WelcomDietAdviser") 51 binding.textView2.setText("まずは基礎代謝を計算しましょう!") 52 53 } 54 55 // ラジオボタンが押されたときの処理 56 // 男女どちらが選択されたかを man_is_checked, woman_is_checked に保存 57 fun onClickRadioButton(view: View) { 58 59 if (view is RadioButton) { // RadioButton からしか呼ばれないのでもはや不要 60 val checked = view.isChecked 61 62 when (view.getId()) { 63 R.id.man -> if (checked) { 64 man_is_checked = true 65 woman_is_checked = false 66 } 67 R.id.woman -> if (checked) { 68 man_is_checked = false 69 woman_is_checked = true 70 } 71 } 72 } 73 } 74 75 // 計算ボタンが押されたときの処理 76 //kisotaisya(Man) = 13.397 * weight + 4.799 * hight - 5.677 * age + 88.362 77 //kisotaisya(Woman) = 9.247 * weight + 3.098 * hight - 4.33 * age + 447.593 78 fun onClickCalcButton(view: View) { 79 80 weight = editWeight.text.toString().toFloatOrNull() 81 age = editAge.text.toString().toFloatOrNull() 82 height = editHight.text.toString().toFloatOrNull() 83 84 //3つの内どれかがnullであればダイアログを表示させる処理 85 if (weight == null || age == null || height == null) { 86 AlertDialog.Builder(this) 87 .setTitle(R.string.dialog_title_invalid_input) 88 .setPositiveButton(android.R.string.ok, null).show() 89 return 90 } 91 92 var kisotaisya: Float 93 if (man_is_checked) { // 男が選ばれている 94 kisotaisya = 13.397f * weight!! + 4.799f * height!! - 5.677f * age!! + 88.362f 95 } else if (woman_is_checked) { // 女が選ばれている 96 kisotaisya = 9.247f * weight!! + 3.098f * height!! - 4.33f * age!! + 447.593f 97 } else { 98 // 男も女も選ばれていない 99 AlertDialog.Builder(this) 100 .setTitle(R.string.dialog_title_invalid_input) 101 .setPositiveButton(android.R.string.ok, null).show() 102 return 103 } 104 105 binding.kisotaisya.setText(kisotaisya.toString()) 106 } 107 108 // 記録ボタンが押されたときの処理 109 fun onClickSaveButton(view: View) { 110 111 val data: SharedPreferences = getSharedPreferences("DATA", Context.MODE_PRIVATE) 112 val editor = data.edit() 113 editor.putFloat("WEIGHT", weight!!) 114 editor.putFloat("AGE", age!!) 115 editor.putFloat("HEIGHT", height!!) 116 editor.apply() 117 } 118}

xml

1<?xml version="1.0" encoding="utf-8"?> 2... 3 <RadioButton 4 android:id="@+id/man" 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" 7 android:text="" 8 android:onClick="onClickRadioButton" 9 app:layout_constraintBottom_toTopOf="@+id/kisotaisya" 10 app:layout_constraintEnd_toEndOf="parent" 11 app:layout_constraintHorizontal_bias="0.37" 12 app:layout_constraintStart_toStartOf="parent" 13 app:layout_constraintTop_toBottomOf="@+id/hight" /> 14 15 <RadioButton 16 android:id="@+id/woman" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="" 20 android:onClick="onClickRadioButton" 21 app:layout_constraintBottom_toBottomOf="@+id/man" 22 app:layout_constraintEnd_toEndOf="parent" 23 app:layout_constraintHorizontal_bias="0.65" 24 app:layout_constraintStart_toStartOf="parent" /> 25 26 </RadioGroup> 27 28 <Button 29 android:id="@+id/infoBtn" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="情報を記録する" 33 android:onClick="onClickSaveButton" 34 app:layout_constraintBottom_toTopOf="@+id/nextBtn" 35 app:layout_constraintEnd_toEndOf="parent" 36 app:layout_constraintStart_toStartOf="parent" 37 app:layout_constraintTop_toBottomOf="@+id/kisotaisya" /> 38 39 <Button 40 android:id="@+id/keisanButton" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:text="計算する" 44 android:onClick="onClickCalcButton" 45 app:layout_constraintBottom_toBottomOf="parent" 46 app:layout_constraintEnd_toStartOf="@+id/nextBtn" 47 app:layout_constraintStart_toStartOf="parent" 48 app:layout_constraintTop_toBottomOf="@+id/kisotaisya" 49 app:layout_constraintVertical_bias="0.79" />

投稿2021/02/13 12:08

ak.n

総合スコア287

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yasuun

2021/02/20 00:14

出血大サービスありがとうございます!! ベストアンサーにさせていただきました! またお答え頂いた後に恐縮なのですが、調べても不明な点を質問させていただきたく、、 ・weight!!、についている!!はNull許容のためでしょうか? ・各3つのメソッドをonCrete内で実装しようとした時に、binding.infoBtn.setOnClickListener { onClickSaveButton() }と書きましたがエラーになります。()内の引数などの問題でしょうか? 質問ばかりで大変申し訳ございません、、よろしくお願い致します。
ak.n

2021/02/20 11:26

1つ目の回答です。 weight の型は、Float ではなく Float? で宣言されています。 この weight を使って、基礎代謝を計算するためには、Float? のままでは計算ができないので、 Float に変換してあげる必要があります。 計算の前段で、weight が null でないことをif ブロックで確認していますので、 「weight は Float?型だけど、前段で null でないことを確認しているから、 Float って認識でよろしくね」 という宣言をしているのが、「weight!!」です。 最初から、weight を Float で宣言すれば、 こんな面倒なことをしなくても済むのに、 わざわざ Float? とした理由は、 エディットボックスから値を取得するときに、.toFloatOrNull() を使いたかったからです。 この関数は、エディットボックスに入力した文字列が数字でない場合など、Float への変換に失敗すると、 null を返してくれるので、null チェックをすれば、「値が不正」というエラーを出せます。 そのため、null を代入できる Float? で宣言したのです。 2つ目の回答です。 onClickSaveButton() の引数 View が無いからです。 onClickSaveButton() のところに、関数の中身を書いてしまうのが早いと思うのですが、 別の手もあります。 xml のボタンの定義から、 android:onClick="onClickCalcButton" を削除して、 class MainActivity : AppCompatActivity() , View.OnClickListener { override fun onCreate(savedInstanceState: Bundle?) { //(途中省略) binding.infoBtn.setOnClickListener(this) binding.keisanButton.setOnClickListener(this) binding.nextBtn.setOnClickListener(this) } override fun onClick(view: View?) { var id = view?.getId() // id を使って処理を分ける } } 3つのボタンが押されると、すべて onClick に飛んできますので、 view の id などをみて、どのボタンが押されたのかチェックして、 処理を分けることが可能です。
yasuun

2021/02/26 01:21

何度もご丁寧に回答いただきありがとうございます、、! 2種類の実装方法を試しながら、次の画面も作成していきたいと思います。 本当にありがとうございます!!
guest

0

計算以前に、これってそもそも起動しませんよね? 前回の回答

また、weighttext, agetext, highttext はフィールドで、かつ binding から初期化してますが、インスタンス生成ににはまだ binding が存在しませんので、実行できたとしても実行時エラーになるはずです。

と書きましたが、まずこの部分を何とかする必要があります。

Kotlin

1class MainActivity : AppCompatActivity() { 2 3 //下記3つはEditText型 4 var editWeight = binding.weight 5 var editAge = binding.age 6 var editHight = binding.hight 7 8 // 略 9 10 private lateinit var binding: ActivityMainBinding 11 12 override fun onCreate(savedInstanceState: Bundle?) { 13 super.onCreate(savedInstanceState) 14 setContentView(R.layout.activity_main) 15 binding = ActivityMainBinding.inflate(layoutInflater) 16 setContentView(binding.root) 17 18 // 略

えっと、editWeight, editAge, editHight および binding はプロパティです。(前回はフィールドと書きましたが、それは Java 用語でした。Kotlin ではプロパティになります。)
参考: プロパティとフィールド - Kotlin Programming Language

var editWeight = binding.weight のように、プロパティに初期値を書いた場合、そのクラスのインスタンスを生成した時に初期化されます。ところが、binding は lateinit なので、インスタンス生成時にはまだ null です。このため、binding.weight がエラーになって起動できないはずです。
参考: 遅延初期化プロパティ

また、この binding というのはビューバインディングという機能で、layout xml に書いた <EditText android:id="@+id/weight" 〜 などの id の値を使って binding.weight などと書けばビューにアクセスできるようになる機能で、これを使わない場合はいちいち findViewById する必要があります。
参考: ビュー バインディング | Android デベロッパー | Android Developers

Android アプリが起動する時には、まず MainActivity のインスタンスが生成され、その後 onCreate メソッドが呼ばて、その中で実際にビューが作られます。ので、editWeight などの初期化は onCreate メソッドが呼ばれるまで遅延する必要があります。
参考: アクティビティのライフサイクルについて | Android デベロッパー | Android Developers

このためには、

Kotlin

1class MainActivity : AppCompatActivity() { 2 3 // ここでは lateinit として宣言 4 lateinit var editWeight: EditText 5 lateinit var editAge: EditText 6 lateinit var editHight: EditText 7 8 // 略 9 10 private lateinit var binding: ActivityMainBinding 11 12 override fun onCreate(savedInstanceState: Bundle?) { 13 super.onCreate(savedInstanceState) 14 setContentView(R.layout.activity_main) 15 binding = ActivityMainBinding.inflate(layoutInflater) 16 setContentView(binding.root) 17 18 // editWeight などはここで初期化 19 editWeight = binding.weight 20 editAge = binding.age 21 editHight = binding.hight 22 23 // 略

とする必要がありますが、個人的にはそもそも editWeight などをプロパティとする必要は感じない (毎回 binding.weight などとしてアクセスすれば良い) ので、プロパティは削除してしまって良いのではないかと思います。

それから、onCreate の中で setContentView(R.layout.activity_main) の後に binding = ActivityMainBinding.inflate(layoutInflater) および setContentView(binding.root) してますが、setContentView を 2 回呼ぶ必要はないので、最初の setContentView(R.layout.activity_main) は削除しましょう。

Kotlin

1class MainActivity : AppCompatActivity() { 2 3 // binding 以外のプロパティは削除 4 5 private lateinit var binding: ActivityMainBinding 6 7 override fun onCreate(savedInstanceState: Bundle?) { 8 super.onCreate(savedInstanceState) 9 10 binding = ActivityMainBinding.inflate(layoutInflater) 11 setContentView(binding.root) 12 13 binding.welcomDA.setText("WelcomDaietAdviser") 14 binding.textView2.setText("まずは基礎代謝を計算しましょう!") 15 } 16 17 fun onClickCalcButton(view: View) { 18 // editWeight などの代わりに binding.weight などを使う 19 val weight2 = binding.weight.text.toString().toFloat() 20 val age2 = binding.age.text.toString().toFloat() 21 val hight2 = binding.hight.text.toString().toFloat() 22 23 // 略

なお、質問の趣旨は RadioButton の状態に基づいて男、女でそれぞれ異なる計算処理を行うことだと思いますが、現状はそもそもアプリが動いてないはずなので、まずは動くようにしましょう。
また、計算処理は次のアクティビティ (ResultActivity1) で行うつもりだと思いますが、そのためには WEIGHT, AGE, HIGHT に加えてもう一つの情報を渡す必要があります。

投稿2021/02/13 11:33

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yasuun

2021/02/14 00:29

今回もご丁寧に回答頂きありがとうございます。 また、bindingの説明も理解できました!!前回と重複したミスになり恐縮です、、。 まずはアプリを起動できるように見直してみます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問