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

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

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

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

Kotlin

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

Q&A

解決済

1回答

704閲覧

kotlinでボタンにIDを設定して処理を実行したい

suzuki_827

総合スコア2

Android Studio

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

Kotlin

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

0グッド

1クリップ

投稿2022/09/29 05:12

前提

AndroidStudioで座標計算アプリを作りたいです。
今は一行分のみですが、座標は4点分まで増やしたいです。

実現したいこと

右のCボタンを押すと、その行の座標①、x,y座標に入力したテキストを削除する。

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

Unresolved reference: btn Unresolved reference: Coordinate1(x1,y1の箇所にもエラー) Variable expected

該当のソースコード

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="test" 13 android:textSize="30sp" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" 18 app:layout_constraintVertical_bias=".05" 19 tools:ignore="HardcodedText" /> 20 21 <EditText 22 android:id="@+id/Coordinate1" 23 android:inputType="text" 24 android:hint="座標①" 25 android:textSize="15sp" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 app:layout_constraintBottom_toBottomOf="parent" 29 app:layout_constraintHorizontal_bias="0.04" 30 app:layout_constraintLeft_toLeftOf="parent" 31 app:layout_constraintRight_toRightOf="parent" 32 app:layout_constraintTop_toTopOf="parent" 33 app:layout_constraintVertical_bias="0.15" 34 tools:ignore="Autofill,HardcodedText" /> 35 36 <EditText 37 android:id="@+id/Coordinate_x1" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:hint="①x座標" 41 android:inputType="numberDecimal" 42 android:textSize="15sp" 43 app:layout_constraintBottom_toBottomOf="parent" 44 app:layout_constraintHorizontal_bias="0.3" 45 app:layout_constraintLeft_toLeftOf="parent" 46 app:layout_constraintRight_toRightOf="parent" 47 app:layout_constraintTop_toTopOf="parent" 48 app:layout_constraintVertical_bias="0.15" 49 tools:ignore="Autofill,HardcodedText" /> 50 51 <EditText 52 android:id="@+id/Coordinate_y1" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:hint="①y座標" 56 android:inputType="numberDecimal" 57 android:textSize="15sp" 58 app:layout_constraintBottom_toBottomOf="parent" 59 app:layout_constraintHorizontal_bias="0.8" 60 app:layout_constraintLeft_toLeftOf="parent" 61 app:layout_constraintRight_toRightOf="parent" 62 app:layout_constraintTop_toTopOf="parent" 63 app:layout_constraintVertical_bias="0.15" 64 tools:ignore="Autofill,HardcodedText" /> 65 66 <Button 67 android:id="@+id/btn" 68 android:layout_width="20pt" 69 android:layout_height="20pt" 70 android:textSize="15sp" 71 android:text="C" 72 app:layout_constraintBottom_toBottomOf="parent" 73 app:layout_constraintHorizontal_bias="1" 74 app:layout_constraintLeft_toLeftOf="parent" 75 app:layout_constraintRight_toRightOf="parent" 76 app:layout_constraintTop_toTopOf="parent" 77 app:layout_constraintVertical_bias="0.15" /> 78 79 80</androidx.constraintlayout.widget.ConstraintLayout> 81

MainActivity.kt

1package com.example.test 2 3import android.os.Bundle 4import androidx.appcompat.app.AppCompatActivity 5 6class MainActivity : AppCompatActivity() { 7 override fun onCreate(savedInstanceState: Bundle?) { 8 super.onCreate(savedInstanceState) 9 setContentView(R.layout.activity_main) 10 btn.setOnClickListener{ 11 Coordinate1.text = "" 12 Coordinate_x1.text = "" 13 Coordinate_y1.text = "" 14 15 } 16 } 17} 18 19 20

試したこと

https://qiita.com/K4N4/items/9bab86f87883f90baa15
https://codeforfun.jp/android-studio-how-to-set-button-click-event-with-onclicklistener/
このあたりの記事を参考にしました。

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

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

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

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

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

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

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

konn_

2022/09/30 01:07

ずっとcomposable関数をいじっていて記憶が定かでないのですが、findViewById使用しなくてもID呼び出せてるんですか?
suzuki_827

2022/09/30 03:06

回答を参考にしてfindViewByIdを追記しました。 ただその箇所ではエラーが起きていないのですが、findViewByIdの後に btn1.setOnClickListener { Coordinate1.text = "" Coordinate_x1.text = "" Coordinate_y1.text = "" } 空白にするためこのように記入したところ 「""」の箇所に Type mismatch: inferred type is String but Editable! was expected とエラーが起きています。 textviewになにか入力する際はこのような記述だったと思うのですが、editTextだと(もしくは入力タイプによって)記述方法変化しますでしょうか?
konn_

2022/09/30 03:28

大変失礼しました。回答編集させていただきました。 editTextに入力されるのはtextviewですので、型はTextViewですね。
suzuki_827

2022/09/30 05:15

val btn1 = findViewById<Button>(R.id.btn1) val coordinate1 = findViewById<EditText>(R.id. coordinate1) val coordinatex1 = findViewById<EditText>(R.id.coordinate_x1) val coordinatey1 = findViewById<EditText>(R.id.coordinate_y1) この部分ではEditTextで問題ないです。このまま試しに btn1.setOnClickListener { Toast.makeText(applicationContext, "トーストメッセージ", Toast.LENGTH_LONG).show() } にしたところトーストメッセージは出現しましたのでボタンのID等も取得できています。 現状座標を入力するEditTextの箇所の文字入力を削除する処理で coordinate1.text = "" と入力していますが 型が一致しません。 必要: Editable! 検出: String と出てきています。
konn_

2022/09/30 05:32

エラー文に、型の不一致とありますよね。 EditTextが型になっていますが、TextViewでないとエラーがおきます。 先程回答編集したとおりに、TextView型にするとエラーはなくなると思います。
suzuki_827

2022/09/30 05:58

そちらの変更ということでしたか、ありがとうございます。 無事解決したのでベストアンサーにさせていただきます!
guest

回答1

0

ベストアンサー

IDを呼び出せていないので、IDを呼び出す必要があります。
いくつか方法はあるのですが、簡単なのはfindViewByIdです。

val btn = findViewById<Button>(R.id.btn) val Coordinate1 = findViewById<TextView>(R.id. Coordinate1) val Coordinate_x1 = findViewById<TextView>(R.id.Coordinate_x1) val Coordinate_y1 = findViewById<TextView>(R.id.Coordinate_y1)

こんな感じだったでしょうか。
setContentViewの下辺りに貼り付けておいたらいいと思います。
間違えていたらすみません

投稿2022/09/30 01:20

編集2022/09/30 03:26
konn_

総合スコア28

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問