changer le texte d'un TextView en fonction de l'image de fond d'un ImageButton
Bonjour.
Je vous sollicite pour résoudre un problème auquel je ne trouve pas de solution même après avoir 'googlé' maintes fois.
J'ai créé un projet fait d'une seule Activity, contenant 2 boutons, un ImageButton et un TextView.
L'mage de fond de ImageButton change quant je clique sur un bouton ou sur l'autre (image "Garçon" ou "Fille"). Là pas de problème.
Fichier XML:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa2536"
android:layout_marginTop="40dp"
android:text="Garçon"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="25dp"
android:id="@+id/btnGarcon">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa2536"
android:layout_marginTop="20dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="25dp"
android:text="Fille"
android:id="@+id/btnFille">
</Button>
<ImageButton
android:id="@+id/btnImage"
android:layout_width="222dp"
android:layout_height="222dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
>
</ImageButton>
<TextView
android:id="@+id/lblNom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:hint="Nom"
android:gravity="center">
</TextView>
</LinearLayout> |
Fichier kt:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.stracoma.boutonimage
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnGarcon.setOnClickListener {
btnImage.setBackgroundResource(R.drawable.garcon)
}
btnFille.setOnClickListener {
btnImage.setBackgroundResource(R.drawable.fille)
}
}
} |
Mon problème est que je veux avoir lblNom.text="Fille" quand je clique sur l'ImageButton avec image de fond ="fille" et avoir lblNom.text="Garçon" quant je clique sur l'ImageButton avec image de fond ="garcon"
je ne veux pas cette solution:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnGarcon.setOnClickListener {
btnImage.setBackgroundResource(R.drawable.garcon)
lblNom.text="Garçon"
}
btnFille.setOnClickListener {
btnImage.setBackgroundResource(R.drawable.fille)
lblNom.text="Fille"
}
}
} |
je veux changer lblNom.text par le biais de btnImage.setOnClickListener().
Merci