diadia

興味があることをやってみる。自分のメモを残しておきます。

ボタンを押すとTextViewに"PUSH"と表示させる

リスナとイベントハンドラの使い方

リスナはイベントが起きるのをじっと待機するもので、イベントハンドラはイベントが起きたと伝達を受けたとき、何らかの処理を起こすもの(何らかの処理は自分がコードを書いて処理を規定する)。
今回はTextViewに"待っている"と表示させておいて、ボタンが押されたときTextViewの内容を"PUSH"に変更する仕組みをどのように構築するかメモする。
イベントハンドラとリスナはktファイルに書くものである。ktファイルを除いたものだけでまずボタン、テキストビューを表示させる。表示できたらイベントハンドラとリスナを書き、動きがあるようにしようと思う。

.ktファイルを除いてファイルを記述する

res/layout/activity_main.xml


<TextView
    andorid:id="@+id/tvMessage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/tv_message"/>
    
<Button
    android:id="@+id/btClick"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/bt_click"/>
res/values/strings.xml

<string name="tv_message">待っている </string>
<string name="bt_click">button </string>
    

これでテキストビューとボタンを表示することが出来た。これにイベントハンドラとリスナを追加していく。

イベントハンドラとリスナの追加

イベントハンドラとリスナは独立した関係ではなく、ある関係をもっている。
リスナを承継したクラスをリスナとし、そのリスナのメソッドとして処理を記述する。つまり承継したリスナのメソッドがイベントハンドラという関係をもつ。 そしてリスナのインスタンスを画面部品(ビュー)に設置する。これはsetOnClickListenerで設置する。
リスナについてはViewを調べると良い。
https://developer.android.com/reference/kotlin/android/view/View

A View occupies a rectangular area on the screen and is responsible for drawing and event handling.

Viewは画面上の長方形の領域を占有し、描画とイベント処理を担当します。とあるように、Viewにリスナが入っている。だからリスナから継承してサブクラスを作り、イベントハンドラの動きを規定するにはViewからリスナを引っ張り出す。そのソースは以下にある。

Once you have created a tree of views, there are typically a few types of common operations you may wish to perform: ... Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using setOnFocusChangeListener(android.view.View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked.

View.onClickListener()のサブクラスを生成し、onClick(view:View)をオーバーライドする。そしてこのサブクラスのインスタンスを生成する。ViewオブジェクトのsetOnClickListener()メソッドに先程のサブクラスのインスタンスを引数として渡す。

MainActivity.kt


class MainActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bandle?){
        super,onCreate(SavedInstanceState)
        setContentView(R.layout.activity_main)
        
        val btn = findViewById<Button>(R.id.btClick)
        listener= ButtonListener()
        btn.setOnClickListener(listener)
        }
    
    
    
    private inner class ButtonListener: View.OnClickListener{
        override fun onClick(view: View){
            val tv = findViewById<TextView>(R.id.tv_message)
            tv.text = "PUSH"
            }
        }
    }
    

        

補足:TextViewのメッセージ内容を変更する方法を上記でtv.text="hoge"と書いていたけど、その他の書き方としてtv.setText("hoge")という書き方もあるようだ。
参考:https://buildbox.net/kotlin-textview-property-method/

参考URL:https://developer.android.com/reference/kotlin/android/view/View.OnClickListener.html
https://developer.android.com/reference/kotlin/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)