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

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

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

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

Kotlin

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

Q&A

解決済

1回答

2699閲覧

kotlinでonClickを設定したら黄色い忠告(Use databinding or explicit~)が表示されたんですが、それの対処法が知りたい

imatya999

総合スコア38

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2021/06/20 00:30

編集2021/06/20 23:25

前提・実現したいこと

簡単な〇×クイズみたいなコードを書けるように勉強しています。
ボタンをおしたら正誤判定するように、ボタンそれぞれに
android:onClick="checkAnswer"
と設定しているのですが

Use databinding or explicit wiring of click listenter in code ●●

と表示されまして。
一応、無視してもエミュレータは正常に動くので致命傷ではなさそうなのですが
今後、Button選択肢を増やすことを想定した場合、
何が原因で、どう書き直せばいいのか、ちゃんと対処法を知っておきたい

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

Use databinding or explicit wiring of click listenter in code ●●

該当のソースコード

xml

1<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity"> 7 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_marginTop="32dp" 14 android:text="Question" 15 android:textSize="34sp" 16 app:layout_constraintEnd_toEndOf="parent" 17 app:layout_constraintStart_toStartOf="parent" 18 app:layout_constraintTop_toTopOf="parent" /> 19 20 <Button 21 android:id="@+id/button0" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_marginTop="32dp" 25 android:onClick="checkAnswer" 26 android:text="text" 27 app:layout_constraintEnd_toEndOf="parent" 28 app:layout_constraintStart_toStartOf="parent" 29 app:layout_constraintTop_toBottomOf="@+id/textView" /> 30 31 <Button 32 android:id="@+id/button1" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_marginTop="32dp" 36 android:onClick="checkAnswer" 37 android:text="text" 38 app:layout_constraintEnd_toEndOf="parent" 39 app:layout_constraintStart_toStartOf="parent" 40 app:layout_constraintTop_toBottomOf="@+id/button0" /> 41</androidx.constraintlayout.widget.ConstraintLayout>

kotlin

1import androidx.appcompat.app.AppCompatActivity 2import android.os.Bundle 3import android.view.View 4import android.widget.Button 5import android.widget.TextView 6 7 8class MainActivity : AppCompatActivity() { 9 10 private val quizData = arrayOf("answer0","answer1") 11 12 override fun onCreate(savedInstanceState: Bundle?) { 13 super.onCreate(savedInstanceState) 14 setContentView(R.layout.activity_main) 15 16 //中略 17 } 18 19 fun checkAnswer(view: View){ 20 val textView:TextView = findViewById(R.id.textView) 21 val touchBtn = view as Button 22 23 if(touchBtn.text==quizData[0]){ 24 textView.text="正解" 25 26 }else{ 27 textView.text="不正解" 28 29 } 30 } 31}

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

AndroidStudio4.2.1

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

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

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

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

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

hoshi-takanori

2021/06/20 01:42

onCreate で button0.setOnClickCListener { 〜 } のように書けってことだと思いますが、別にその書き方でも間違いではないと思います。
imatya999

2021/06/20 02:03

ありがとうございます。 間違いではない、とのことでひとまず安心しました。 >button0.setOnClickCListener { 〜 } のように書け ただ↑こうなると、2つほどわからないのですが 1)onClick="checkAnswer"は使うな、ということになるのでしょうか? 2)今は2つですが、ボタンが例えば5つくらいになった場合 button0.setOnClickCListener {~} button1.setOnClickCListener {~} button2.setOnClickCListener {~} button3.setOnClickCListener {~} button4.setOnClickCListener {~} みたいに、どんどん増殖していく感じになるんでしょうか? (ちょっとonCreate内での記述イメージがわかないので、もう少しお伺いしたく) よろしくお願いいたします。
hoshi-takanori

2021/06/20 03:44

Kotlin 側で setOnClickCListener するなら onClick="〜" は不要になります。 今後の方向性としては、ボタン (つまり答えの選択肢) が増えるだけじゃなくて、問題自体も増えるんですよね。 選択肢の数が変わったりもする予定ですか? 自分だったらとりあえず checkAnswer の引数は Int (答えの番号) にして、 button0.setOnClickListener { checkAnswer(0) } button1.setOnClickListener { checkAnswer(1) } button2.setOnClickListener { checkAnswer(2) } みたいにするかな…。
imatya999

2021/06/20 23:27

ありがとうございました。 参考にさせていただきます。
guest

回答1

0

自己解決

書き方そのものは間違っていないようでした。
お騒がせしました。

投稿2021/06/20 23:27

imatya999

総合スコア38

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問