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

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

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

HTMLで用いる<button>タグです。

Kotlin

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

Q&A

1回答

2076閲覧

kotlin ボタンクリック時のtextviewリセット

ryo_kato

総合スコア4

button

HTMLで用いる<button>タグです。

Kotlin

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

0グッド

0クリップ

投稿2020/02/21 02:56

前提・実現したいこと

ボタンクリック時にtextviewをエミュレーター起動時の状態に戻したいです。

現在、BUTTONが3つあり、そのうち2つはtextviewの表示内容を変えるもの、もう一つはリセットボタンとして扱いたいのですができません。

デフォルトとして、元の状態を保管し、ボタン3クリック時にあらかじめ保管しておいたものを組み込むというような方で処理したいです。

宜しくお願い致します。

該当のソースコード

package com.example.words20200219

import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var textView = findViewById<TextView>(R.id.Words) var countView=findViewById<TextView>(R.id.count) var button1 = findViewById<Button>(R.id.button1) var button2=findViewById<Button>(R.id.button2) var button3=findViewById<Button>(R.id.button3) var count: Int = 0 button1.setOnClickListener { count++ textView.setTextColor(Color.WHITE) textView.setBackgroundColor(Color.DKGRAY) when { (textView.getText().toString() == "Hello World") -> textView.setText(R.string.words_android) (textView.getText().toString() == "Hello Android") -> textView.setText(R.string.words_japan) (textView.getText().toString() == "Hello Japan") -> textView.setText(R.string.words_world) }} button2.setOnClickListener { countView.setText(count.toString()) countView.setTextColor(Color.RED) } button3.setOnClickListener{ } }

}

ソースコード

試したこと

ここに問題に対して試したことを記載してください。

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

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

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

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

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

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

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

hoshi-takanori

2020/02/21 07:10

アプリ起動時の textview の状態を覚えておいて、リセットボタンが押されたら元に戻せばいいのでは。
nakasho_dev

2020/02/22 15:34

「~処理したいです」ということなので、まずはその処理にするように試して、『試したこと』に記述してみてはいかがでしょうか。
guest

回答1

0

nakasho_dev様のコメントの提案をコードにするとこうなりますね。

Kotlin

1package com.example.words20200219 2 3import android.graphics.Color 4import android.os.Bundle 5import android.widget.Button 6import android.widget.TextView 7import androidx.appcompat.app.AppCompatActivity 8 9class MainActivity : AppCompatActivity() { 10 11 // 初期値を格納する変数を追加 12 private lateinit var textViewTextCache: String 13 private var textViewColorCache = 0 14 15 private lateinit var countViewTextCache: String 16 private var countViewColorCache = 0 17 18 override fun onCreate(savedInstanceState: Bundle?) { 19 super.onCreate(savedInstanceState) 20 setContentView(R.layout.activity_main) 21 22 var textView = findViewById<TextView>(R.id.Words) 23 // 初期値を格納 24 textViewTextCache = textView.text 25 textViewColorCache = textView.textColor 26 27 var countView=findViewById<TextView>(R.id.count) 28 // 初期値を格納 29 countViewTextCache = countView.text 30 countViewColorCache = countView.textColor 31 32 var button1 = findViewById<Button>(R.id.button1) 33 var button2=findViewById<Button>(R.id.button2) 34 var button3=findViewById<Button>(R.id.button3) 35 36 var count: Int = 0 37 38 button1.setOnClickListener { 39 count++ 40 textView.setTextColor(Color.WHITE) 41 textView.setBackgroundColor(Color.DKGRAY) 42 43 when { 44 (textView.getText().toString() == "Hello World") -> 45 textView.setText(R.string.words_android) 46 47 (textView.getText().toString() == "Hello Android") -> textView.setText(R.string.words_japan) 48 49 (textView.getText().toString() == "Hello Japan") -> textView.setText(R.string.words_world) 50 } 51 } 52 53 button2.setOnClickListener { 54 countView.setText(count.toString()) 55 countView.setTextColor(Color.RED) 56 } 57 58 button3.setOnClickListener{ 59 // 保持してあった値を適用 60 textView.text = textViewTextCache 61 textView.textColor = textViewColorCache 62 63 countView.text = countViewTextCache 64 countView.textColor = countViewColorCache 65 } 66 } 67}

このやり方だと、あくまでレイアウトXMLで設定した値が保持されて、再適用されます。

変更後の値を複数回の実行において、持ち越したりしたい場合は、SharedPreferencesなどを用いて永続化する必要があります。

投稿2020/03/02 06:26

編集2020/03/02 06:27
quadii.shii

総合スコア257

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問