Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Résolu] Textview: Changer la couleur quand je clique dessus

Featured Replies

Posté(e)

Salut!

J'ai un LinerLayout avec une TextView et une ImageView J'aimerais faire la meme chose qu'avec un bouton: wquand on cliques, le background change. J'arrive a faire ca quand je focus sur l'objet (avec la trackball), mais pas quand je clique :(

J'ai essaye plein de choses differentes, mais rien n'a fonctionne... Des idees?

merci d'avance

Mon Linear Layout:

        android:orientation="horizontal"
       android:id="@+id/widgetProvider"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:focusableInTouchMode="true"
       android:clickable="true"
       android:layout_margin="1dip"
       android:background="@drawable/textview">

           android:id="@+id/imgProvider"
           android:layout_width="60dip"
           android:layout_height="fill_parent"
           android:layout_marginRight="5dip"
           android:src="@drawable/icon" />

               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">


Mon background qui change (textview.xml)





Modifié par Z!dd0

Posté(e)

Fait le avec un bouton plutôt qu'un textview.

Ensuite dans ton code tu rajoutes.

Imaginons que ton bouton se nomme : bouton

bouton.setBackgroundResource(R.***.textview);

Remarque ca doit aussi fonctionner avec une textview ;)

Posté(e)
  • Auteur

Dans mon fichier layout, je peux pas mettre bouton a la place de "textview"!

Posté(e)

Ptetre parce qu'il faut l'écrire "button" ?

Posté(e)
  • Auteur

Comme ca tu veux dire???

        android:orientation="horizontal"
       android:id="@+id/widgetProvider"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:focusableInTouchMode="true"
       android:clickable="true"
       android:layout_margin="1dip"
       android:background="@drawable/textview">

           android:id="@+id/imgProvider"
           android:layout_width="60dip"
           android:layout_height="fill_parent"
           android:layout_marginRight="5dip"
           android:src="@drawable/icon" />

               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">


Posté(e)

Oui comme ça en séparant bien :

                            android:id="@+id/btnProvider" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" 
               android:maxHeight="10dip"
               android:text="@string/provider"
               android:gravity="center_vertical">

Ensuite il faut que tu codes ton click avec un OnClickListener

Tu peux le faire avec un TextView aussi puisque TextView dérive de View qui a OnClickListener (Button dérive de TextView aussi :p)

Modifié par Fluckysan

Posté(e)

Je confirme ce que dit FluckySan, d'ailleurs c'est la même idée, entre ImageButton et ImageView :)

Posté(e)

Tout dépend l'utilisation en effet.

Z!dd0 : il faudrait que tu nous dises ce que tu souhaites faire exactement. TextView peut s'avérer être suffisant pour tes besoins.

Posté(e)
  • Auteur

Merci a tous de vos reponses :)

En fait, on peut voir dans le layout que mon element principal se compose d'une image (a gauche) et d'une zone de texte (a droite). Basiquement, c'est ca:

*********************************

| | |

| Image | Texte Texte |

| | |

*********************************

Ce que je veux, c'est que quand je clique sur le tout (n'importe ou), cela fonctionne comme un bouton et ca fasse une action.

J'arrive a faire ca, sauf que cet element ne se highlighte pas. Il ne change pas de couleur quand on appuie dessus alors que j'ai bien mentionne dans le layout le "state_pressed"

J'espere que vous comprenez un peu mieux mon probleme ;)

Posté(e)

Bah il suffit d'utiliser un Bouton et de lui affecter une image avec l'attribut drawableLeft :

                                       android:drawableLeft="@android:drawable/ic_menu_add"
                                       android:layout_width="fill_parent"
                                       android:layout_gravity="center_horizontal"
                                       android:id="@+id/BtnCreateAlbum" />

non ?

Posté(e)
  • Auteur

Oui, je pourrais utiliser ca, mais les images que j'affiche sont dynamiques et vienennt du net, donc je prefererai utiliser une imageview...

Je ne vois vraiment pas pourquoi le "state_pressed" ne donne rien :(

Posté(e)
  • Auteur

Ouais je vais essayer de faire ca, merci.

Mais sinon, il est donc impossible de highlighter un textview?

Posté(e)
  • Auteur

Oui, j'avais jete un oeil la-dessus.

J'ai resolu le probleme: en fait, j'avais un onTouchListener au lieu d'un onClicklistener, et le highlight n'etait pas possible.

Avec le onClickListener, tout se passe bien!

Merci beaucoup!

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.