0
0

More than 3 years have passed since last update.

AndroidStudioで様々なボタンの動作を確認

Posted at

AndroidStudioでボタンの動作を確認します

ボタンのクリックイベント、ボタンのチェック状態を確認するサンプルを作ってみます。

strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="app_name">ButtonSample</string>
    <string name="radio_Udon">うどん</string>
    <string name="radio_Soba">そば</string>
    <string name="check_Age">油揚げ</string>
    <string name="check_Tempura">天ぷら</string>
    <string name="btn_Pay">精算</string>
    <string name="tv_Pay">お支払い</string>
</resources>

うどん、そば屋さんで精算するアプリです。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioUdon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_Udon" />

        <RadioButton
            android:id="@+id/radioSoba"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/radio_Soba" />

        <CheckBox
            android:id="@+id/checkAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="@string/check_Age" />

        <CheckBox
            android:id="@+id/checkTempura"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/check_Tempura" />

        <Button
            android:id="@+id/btPay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:onClick="onClick"
            android:text="@string/btn_Pay" />

        <TextView
            android:id="@+id/tvPay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tv_Pay" />
    </RadioGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

RadioGroupにボタンを入れると勝手に整列するので本来の使い方とは違うと思いますが使いました。

MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun onClick(view : View){
        val radioUdon = findViewById<RadioButton>(R.id.radioUdon)
        val radioSoba = findViewById<RadioButton>(R.id.radioSoba)
        val checkAge = findViewById<CheckBox>(R.id.checkAge)
        val checkTempura = findViewById<CheckBox>(R.id.checkTempura)
        val tvPay = findViewById<TextView>(R.id.tvPay)

        var sum : Int=0
        if (radioUdon.isChecked){sum += 350}
        if (radioSoba.isChecked){sum += 370}
        if (checkAge.isChecked){sum += 150}
        if (checkTempura.isChecked){sum +=200}
        tvPay.setText("合計:$sum 円です")
    }
}

値段は適当です。正常に動くか確認してみましょう。
radioButton、checkButtonともにisCheckedを調べるとボタンのチェック状態がわかります。このプロパティに代入すると表示も変化します。

1つのイベントを複数のボタンから利用する

毎回精算ボタンを押すのは面倒なのでradioButton、checkButtonがクリックされても計算するようにしましょう。
radioButton、checkButtonを全て選択した状態でonClickの値を設定すれば全てに反映されます。
もう精算ボタンは不要なので消してしまいましょう。

イベント発生元を調べる

複数のボタンから同じイベントを呼ぶ場合、呼ばれた側がどのボタンから呼ばれたのか知りたい場合があります。

その場合はイベントの引数として渡されるView型の viewを利用するようです。

view.kt
    fun onClick(view : View){
        val radioUdon = findViewById<RadioButton>(R.id.radioUdon)
        val radioSoba = findViewById<RadioButton>(R.id.radioSoba)
        val checkAge = findViewById<CheckBox>(R.id.checkAge)
        val checkTempura = findViewById<CheckBox>(R.id.checkTempura)
        val tvPay = findViewById<TextView>(R.id.tvPay)

        var sum : Int=0
        if (radioUdon.isChecked){sum += 350}
        if (radioSoba.isChecked){sum += 370}
        if (checkAge.isChecked){sum += 150}
        if (checkTempura.isChecked){sum +=200}
        tvPay.setText("合計:$sum 円です")
        when(view.id) {
            radioUdon.id    -> { Log.d("Sample","うどんが押された") }
            radioSoba.id    -> { Log.d("Sample","そばが押された")   }
            checkAge.id     -> { Log.d("Sample","油揚げが押された") }
            checkTempura.id -> { Log.d("Sample","天ぷらが押された") }
        }
    }

このように viewのidとレイアウト上のidをfindViewByIdで変換した idを比較することで、どこから来たのか判明します。

最後に

今回使用したonClick(view : View)のイベント受け取り方法は引数が view : View のものに限定されるようです。
それ以外はリスナーを使うのですが全てのリスナーを説明するための情報がまだ不十分なので全部揃ったら説明というかメモします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0