2011-04-04 14 views
5

Estoy haciendo una aplicación para Android, tengo 4 textviews, a saber, ProductId, título, descripción, imagen.Quiero cuando haga clic en cada uno de ellos, debe mostrarse la identificación del producto. Tengo un servicio web para esta.haciendo una vista de texto hacer clic en Android

salida del servicio web es

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> 

Gracias de antemano Tushar

+0

¿Cuál es la pregunta aquí? ¿Qué has intentado hasta ahora? –

+0

He creado un xml con 4 textos ... Aquí está mi código java – User

+0

Si lo entendí correctamente, quiere definir un ClickListener para cada TextView, por lo que podría, por ejemplo, mostrar un tostador con el Product ID. – gnclmorais

Respuesta

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

Usted puede simplemente crear OnClickListeners de sus textviews como:

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

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

funciona este detector de clics:

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 vista de texto se declara con referencia a android:clickable="true" (asistente por defecto):

 <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

también puede hacerlo utilizando XML como

 <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" /> 

y manejar onClick like

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

¿Se puede hacer un "clic largo" en XML también? –

+0

no hay ningún método de compilación. Pero aún puedes personalizar tu vista para hacer esto. https://stackoverflow.com/a/13417824/3496570 – Nepster

+0

Bien, gracias por confirmar. Terminó usando 'setOnLongClickListener (new View.OnLongClickListener() {...}' en el archivo de actividad de Java. –

Cuestiones relacionadas