Академический Документы
Профессиональный Документы
Культура Документы
Esta interfaz inicial estará diseñada en una organización de tablas, el diseño lo organizaremos
en filas(elemeto Table Row) y dentro de cada fila pondremos el elemento. Para eso editamos
el archivo Activity_main.xml y añadimos lo siguiente:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="center"
android:text="@string/mensaje"
android:textAllCaps="true"
android:textSize="25px"
android:textStyle="bold" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.27" >
<TableRow
android:id="@+id/tableRow1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:bufferType="editable"
android:padding="10sp"
android:text="@string/nom" />
<EditText
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:completionThreshold="3"/>
<requestFocus />
</TableRow>
<TableRow
android:id="@+id/tableRow2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:bufferType="editable"
android:padding="6sp"
android:text="@string/direccion" />
<EditText
android:id="@+id/direccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:bufferType="editable"
android:padding="9sp"
android:text="@string/tel" />
<EditText
android:id="@+id/telefono"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow4" >
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCancelarClick"
//se ejecuta el método de cancelar en el botón cancel
android:text="@string/cancel" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onAgregarClick"
//se ejecuta el método de agregar en el botón agregar
android:text="@string/add" />
</TableRow>
</TableLayout>
</LinearLayout>
En la carpeta res/values editamos el archivo Strings.xml. Ahí definimos las cadenas constantes
de nuestra aplicación.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">cliente</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world, Form!</string>
<string name="mensaje">Nuevo Cliente</string>
<string name="nom">Nombre</string>
<string name="direccion">Direccion</string>
<string name="tel">Telefono</string>
<string name="cancel">Cancelar</string>
<string name="add">Agregar</string>
<string name="titulo">Resultado</string>
<string name="mensaje2">Exito</string>
</resources>
package com.example.cliente;
import com.example.cliente.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
intent.setComponent(new ComponentName(this,form2.class));
//guarda la cadena cancel en la etiqueta cancel para luego ser utilizada en la actividad form2
intent.putExtra("Cancel","Cancel");
startActivity(intent);
intent.setClass(this,form2.class);
//guardamos las variables en 3 etiquetas para utilizarlas en la actividad form2
intent.putExtra("nombre", xnombre.getText().toString());
intent.putExtra("direccion",xdireccion.getText().toString());
intent.putExtra("telefono",xtelefono.getText().toString());
startActivity(intent);
Ahora creamos un nuevo xml: result.xml donde mostraremos el mensaje de éxito más los
datos del cliente o el mensaje cancelar.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:focusableInTouchMode="false"
android:textSize="25sp"
android:padding="10sp" android:gravity="center"
android:text="@string/titulo" />
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:focusableInTouchMode="false"
android:textSize="20sp"
android:padding="10sp" android:gravity="center"
android:text=""
android:id="@+id/resultText" />
</LinearLayout>
import com.example.cliente.*;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
setContentView(R.layout.result);
if (bundle.getString("Cancel") != null) {
resultText.setText(getString(R.string.cancel));
}
//de lo contrario presionamos el botón agregar
else {
resultText.setText(getString(R.string.mensaje2) +
"\n" +
"Nombre : " + xnombre + "\n" +
"Direccion :" + xdireccion + "\n" +
"Telefono : " + xtelefono);
}
}
private void insertarCliente(String xnombre, String xdireccion,
String xtelefono) {
SQLiteDatabase db = manejoDatabase.getWritableDatabase();
//cerramos la bd
db.close();
}
}
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.cliente.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/titulo"
android:name=".form2" >
</activity>
</application>
</manifest>
package com.example.cliente;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ManejoDatabase extends SQLiteOpenHelper {
//nombre de la base de datos que se creara si no existe
private static final String BASEDATOS = "ventas.db";
//creamos las constantes con los nombres de los campos de la tabla
cliente
public static final String NOMBRE = "nombre";
public static final String DIRECCION = "direccion";
public static final String TELEFONO = "telefono";
//constructor de la clase
public ManejoDatabase(Context context) {
//manejo del numero de versión de la base de datos
super(context, BASEDATOS, null, 1);
}
//cuando se crea por primera vez la base de datos se llama al método
//oncreate donde se creara la bd ventas y la tabla cliente
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE cliente (codigo INTEGER PRIMARY
KEY AUTOINCREMENT NOT NULL , nombre TEXT, direccion TEXT, telefono
TEXT);");
}
//método onupgrade se ejecuta cuando se actualizan las bd a una nueva
//versión elimina la existencia y crea una nueva
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
android.util.Log.w("cliente","Upgrading database, which
will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS cliente");
onCreate(db);
}
}
Listo!
EL ID NO SE MUESTRA EN EL FORMULARIO
valverdebellodasjc@hotmail.com valverdebellodasjc@gmail.com