Last active
October 11, 2015 21:28
-
-
Save pedrojoya/3922445 to your computer and use it in GitHub Desktop.
Entrada Captura de eventos de usuario
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends Activity implements OnClickListener { | |
| // Vistas. | |
| private Button btnBoton; | |
| // Al crear la actividad. | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| btnBoton = (Button) findViewById(R.id.btnBoton); | |
| // La actividad responderác al click sobre el botón. | |
| btnBoton.setOnClickListener(this); | |
| } | |
| // Al hacer click sobre la vista recibida como parámetro. | |
| public void onClick(View v) { | |
| // Dependiendo de la vista sobre la que se ha pulsado. | |
| switch (v.getId()) { | |
| case R.id.btnBoton: | |
| Toast.makeText(this, "Se ha pulsado el botón btnBoton", | |
| Toast.LENGTH_LONG).show(); | |
| break; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends Activity { | |
| // Vistas | |
| private Button btnBoton; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| btnBoton = (Button) findViewById(R.id.btnBoton); | |
| // Se crea un objeto de una clase anónima que implementa la | |
| // interfaz OnClickListener. | |
| btnBoton.setOnClickListener(new OnClickListener() { | |
| public void onClick(View v) { | |
| Toast.makeText(MainActivity.this, "Se ha pulsado el botón btnBoton", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PrincipalActivity extends Activity implements OnClickListener { | |
| // Vistas. | |
| private Button btnBoton; | |
| // Al crear la actividad. | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| btnBoton = (Button) findViewById(R.id.btnBoton); | |
| // Un objeto de la clase interna reponderá al click. | |
| btnBoton.setOnClickListener(new GestorOnClick()); | |
| } | |
| // Clase interna que gestiona el click. | |
| private class GestorOnClick implements OnClickListener { | |
| // Se podria añadir un constructor que recibiera parámetros | |
| // para poder pasar algún valor adicional al listener. | |
| // Al hacer click. | |
| public void onClick(View v) { | |
| Toast.makeText(MainActivity.this, "Se ha pulsado el botón btnBoton", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends Activity { | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| } | |
| // El nombre del método debe coincidir con el valor del | |
| // atributo android:onClick del botón btnBoton. | |
| public void btnBotonOnClick(View v) { | |
| Toast.makeText(this, "Se ha pulsado el botón btnBoton", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:orientation="vertical" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" > | |
| <Button android:id="@+id/btnBoton" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="@string/pulsar" | |
| android:layout_gravity="center_horizontal" | |
| android:onClick="btnBotonOnClick" /> | |
| </LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment