2011-04-04 10 views
5

Je fais une application android, j'ai 4 textviews à savoir ProductId, Titre, description, image.Je veux quand je clique sur chacun d'eux identifiant de produit devrait être affiché.J'ai un webservice pour ce.faire un texte cliquable en android

Sortie de webservice est

vmsc> 
<response code="0" message="Success"/> 
− 
<responsedata> 
− 
<productcategories> 
− 
<productcategory> 
<id>1</id> 
<title>Celebrities</title> 
<description>Celebrities</description> 
<image> 
     </image> 
</productcategory> 
− 
<productcategory> 
<id>2</id> 
<title>Music</title> 
<description>Music</description> 
<image> 
     </image> 
</productcategory> 
− 
<productcategory> 
<id>3</id> 
<title>Sports</title> 
<description>Sports</description> 
<image> 
     </image> 
</productcategory> 
− 
<productcategory> 
<id>4</id> 
<title>Fashion</title> 
<description>Fashion</description> 
<image> 
     </image> 
</productcategory> 
− 
<productcategory> 
<id>5</id> 
<title>Religion</title> 
<description>Religion</description> 
<image> 
     </image> 
</productcategory> 
− 
<productcategory> 
<id>6</id> 
<title>Others</title> 
<description>Others</description> 
<image> 
     </image> 
</productcategory> 
</productcategories> 
</responsedata> 
</vmsc> 

Merci à l'avance Tushar

+0

Quelle est la question ici? Qu'avez-vous essayé jusqu'à présent? –

+0

J'ai créé un fichier XML avec 4 textes .. Voici mon code java – User

+0

Si je l'ai bien compris, vous voulez définir un ClickListener pour chaque TextView, afin qu'il puisse, par exemple, afficher un grille-pain avec l'ID du produit. – gnclmorais

Répondre

17
final TextView view = (TextView) findViewById(R.id.textview1); 
view.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    // request your webservice here. Possible use of AsyncTask and ProgressDialog 
    // show the result here - dialog or Toast 
    } 

}); 
0

Vous pouvez simplement créer OnClickListeners de vos textviews comme:

TextView textview = new TextView(this); 
     textview.setText("This is a textview"); 

     textview.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // do something here. 
      } 
     }); 
0

cet écouteur de clic fonctionne:

setContentView(R.layout.your_layout); 
TextView tvGmail = (TextView) findViewById(R.id.tvGmail); 
String TAG = "yourLogCatTag"; 
tvGmail.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View viewIn) { 
       try { 
        Log.d(TAG,"GMAIL account selected"); 
       } catch (Exception except) { 
        Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage()); 
       } 
      } 
     }); 

la vue texte est déclaré sans référence à android:clickable="true" (assistant par défaut):

 <TextView 
      android:id="@+id/tvGmail" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/menu_id_google" 
      android:textSize="30sp" /> 
3

vous pouvez aussi le faire en utilisant XML comme

 <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="handleOnClick" 
     android:clickable="true" 
     android:text="Clickable TextView" 
     android:textSize="30sp" /> 

et la poignée onCliquer comme

public void handleOnClick(View view) 
{ 
    switch(view.getId()) 
    { 
     case R.id.textView: 
     //do your stufff here.. 
     break; 
     default: 
     break; 
    } 
} 
+0

Pouvez-vous faire un "clic long" en XML aussi? –

+0

il n'y a aucune méthode de construction. Mais vous pouvez toujours personnaliser votre vue pour le faire. https://stackoverflow.com/a/13417824/3496570 – Nepster

+0

D'accord, merci de confirmer. Terminé en utilisant 'setOnLongClickListener (new View.OnLongClickListener() {...}' dans le fichier d'activité Java. –