Tutorial Android: eventos con OnClickListener implementado en un boton
Los eventos los encontramos en muchos lenguajes de programación, para ello en Android tenemos disponibles un gran numero distinto de eventos que podemos tener, desde el touch o clicks, hasta eventos generados por movimientos en un accelerometro, termometro u otros sensores de los dispositivos Android.
En el siguiente video podras encontrar como poder implementar uno de los eventos mas simples, el OnClickListener, para escuchar eventos de clicks realizados sobre un boton.
Videotutorial:
Codigo de apoyo:
package com.practicaOnclick; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class PracticaOnClickListenerActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View boton = findViewById(R.id.botonID); boton.setOnClickListener(this); } @Override public void onClick(View v) { if(v.getId()==findViewById(R.id.botonID).getId()) { TextView texto = (TextView)findViewById(R.id.textoID); texto.setText("Hola que tal, haz oprimido el boton"); } } }
XML del archivo main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textoID" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/botonID" android:layout_width="164dp" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>