ACERTIJO LA RUTA DEL MARATÓN OLÍMPICO DEL NÚMERO PI EN PARÍS. Por JAVIER SOL...
App Android MiniBanco
1. Banco Infantil
En esta práctica vamos a crear un minibanco donde los niños muy peques puedan
ingresar y retirar sus ahorros y practicar con los euros y céntimos de euro.
Para ello desde Eclipse una nueva App Android.
2. MiniBanco.java
package es.bitcoach.appbancorita;
import java.text.DecimalFormat;
import
import
import
import
import
import
import
import
android.os.Bundle;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
android.widget.Toast;
android.app.Activity;
android.content.SharedPreferences;
public class MiniBanco extends Activity {
public static final String MISPREFERENCIAS = "MyPrefsFile";
float cantidad = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mini_banco);
final
final
final
final
final
DecimalFormat formateador = new DecimalFormat("#####0.00");
Button btnIngresar = (Button)findViewById(R.id.btnIngresar);
Button btnRetirar = (Button)findViewById(R.id.btnRetirar);
TextView lblTotal = (TextView)findViewById(R.id.lblTotal);
EditText txtImporte = (EditText)findViewById(R.id.txtImporte);
// Restore preferences
SharedPreferences settings = getSharedPreferences(MISPREFERENCIAS, 0);
cantidad = settings.getFloat("CANTIDAD", 0);
lblTotal.setText(formateador.format (cantidad) + " ");
//-- Ingresar -------------------------------------------------btnIngresar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if(txtImporte.length() == 0){
Toast notificacion = Toast.makeText(getApplicationContext(),
"Pero RITA: Teclea una cantidad en Euros ...",
Toast.LENGTH_SHORT);
notificacion.show();
}else{
cantidad = cantidad + Float.parseFloat(txtImporte.getText().toString());
lblTotal.setText(formateador.format (cantidad) + " ");
txtImporte.setText("");
}
}
});
//-- Ingresar --------------------------------------------------
3. //-- Retirar --------------------------------------------------btnRetirar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if(txtImporte.length() == 0){
Toast notificacion = Toast.makeText(getApplicationContext(),
"Pero RITA: Teclea una cantidad en Euros ...",
Toast.LENGTH_SHORT);
notificacion.show();
}else{
cantidad = cantidad - Float.parseFloat(txtImporte.getText().toString());
lblTotal.setText(formateador.format (cantidad) + " ");
txtImporte.setText("");
}
}
});
//-- Retirar --------------------------------------------------}
@Override
protected void onStop(){
super.onStop();
SharedPreferences settings = getSharedPreferences(MISPREFERENCIAS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat("CANTIDAD", cantidad);
editor.commit();
}
}
En los métodos onCreate (recuperamos) y onStop (almacenamos), configuramos
nuestras preferencias, utilizando la clase SharedPreferences, para almacenar
el valor de la variable “cantidad” que representa la cantidad de euros
disponible en el banco. De esta forma el almacenamiento de este dato será
permanente (no se perderá al apagar la aplicación).
El dato se almacena en el archivo MyPrefsFile.xml que podemos localizar en
Eclipse en la vista DDMS, seleccionando el device correspondiente y
situándose en la pestaña File Explorer:
La ruta: data/data/(paquete)es.bitcoach.appbancorita/shared_prefs.
MyPrefsFile.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<float name="CANTIDAD" value="10.0" />
</map>
5. strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AppBancoRita</string>
<string name="action_settings">Settings</string>
<string name="lblBanco">DINERO QUE TENGO EN EL BANCO</string>
<string name="lblCajero">CAJERO AUTOMÁTICO</string>
</resources>
colores.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="rojo">#ff0000</color>
<color name="verde">#00ff00</color>
<color name="azul">#0000ff</color>
</resources>
Resultado de la ejecución y estructura de la aplicación en Eclipse.