2011-01-06 22 views
11

Estoy construyendo una aplicación de Android que tiene muchos TextViews en los que quiero hacer clic. He intentado asignar las propiedades android:clickable="true" y android:onClick="clickHandler" al único TextView y cuando la aplicación dispara el clickHandler(View v) obtengo el elemento cliqueado correctamente a través del v.getId(). Lo que no me gusta es definir, para cada TextView, las propiedades android:clickable y ... ¿hay algo que pueda hacer por código para decir: "todas las textviews son clicables y el clic se maneja en clickHandler?"Clickable TextView en Android

Gracias.

Respuesta

16

que podría hacer algo como esto abajo - de esta manera que todos ellos tienen el mismo controlador:

public class sticks extends Activity implements View.OnTouchListener { 
    private TextView tv1; 
    private TextView tv2; 
    private TextView tv3; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    tv3 = (TextView) findViewById(R.id.tv3); 

    // bind listeners 
    tv1.setOnTouchListener(this); 
    tv2.setOnTouchListener(this); 
    tv3.setOnTouchListener(this); 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
    // check which textview it is and do what you need to do 

    // return true if you don't want it handled by any other touch/click events after this 
    return true; 
    } 
} 
+0

es el código de trabajo o pseudocódigo? no podría hacer que funcione como está. pero de todos modos! ¡ACTUALIZADO! :) –

11

He utilizado este:

 <TextView 
      android:id="@+id/txtTermsConditions" 
      android:layout_width="180dp" 
      android:layout_height="50dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="10dp" 
      android:onClick="onTermConditions" 
      android:clickable="true" 
      android:text="Terms and Conditions" 
      android:textColor="@color/blue" /> 
+1

En este ejemplo, android: textColorLink no hace nada, a menos que el texto contenga un hipervínculo real. –

+0

Supongo que tienes razón. He editado la respuesta. –

3

Hola tiene que usar ambas por debajo de Código:

<TextView 
     android:id="@+id/reg_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/login_btn" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:clickable="true" 
     android:text="Are you now need to create an account?" 
     android:textColor="#ff0000ff" 
     android:textSize="15sp" 
     /> 

Y

private TextView tv1; 
     tv1= (TextView)findViewById(R.id.reg_text); 
    tv1.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View arg0, MotionEvent arg1) { 
      // TODO Auto-generated method stub 
      ProgressDialog progressBar = ProgressDialog.show(MainActivity.this, "Title", 
        "شکیبا باشید!"); 
      progressBar.setCancelable(true); 
      Intent i = new Intent(getApplicationContext(), Register.class); 
      startActivity(i); 
      return false; 
     } 
    }); 
0

A continuación el código que funcionó para mí:

complemento a strings.xml:

<string name="about_us"><font fgcolor="#039BE5">\"THIS IS SPARTA! more info at" <font fgcolor="#000000"><a href="https://google.com/">google.com</a></font> 
</string> 

añadir a TextView:

android:linksClickable="true" 

añadir a la actividad:

about_us.movementMethod = LinkMovementMethod.getInstance() 
+0

Aún tiene el valor de configuración para cada vista de texto, lo que @Cris quería evitar ... Debería elaborar un poco sobre movementMethod (una solución interesante) – yakobom