Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created October 8, 2016 18:10
Show Gist options
  • Save alvareztech/7d993678476295efb8c8caef64a63635 to your computer and use it in GitHub Desktop.
Save alvareztech/7d993678476295efb8c8caef64a63635 to your computer and use it in GitHub Desktop.
Ejemplo realizado en clases. Sesión 5
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tech.alvarez.formulario.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/nombreTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/nombreEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nombre"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/emailTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/correo_electronico"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/telefonoTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/telefonoEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/telefono"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/femeninoRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/femenino" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/masculino" />
</RadioGroup>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/meses" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:onClick="mostrarMensaje"
android:src="@mipmap/ic_cake_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
package tech.alvarez.formulario;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private EditText nombreEditText;
private EditText emailEditText;
private EditText telefonoEditText;
private TextInputLayout nombreTextInputLayout;
private TextInputLayout emailTextInputLayout;
private TextInputLayout telefonoTextInputLayout;
private RadioButton femeninoRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nombreEditText = (EditText) findViewById(R.id.nombreEditText);
emailEditText = (EditText) findViewById(R.id.emailEditText);
telefonoEditText = (EditText) findViewById(R.id.telefonoEditText);
nombreTextInputLayout = (TextInputLayout) findViewById(R.id.nombreTextInputLayout);
emailTextInputLayout = (TextInputLayout) findViewById(R.id.emailTextInputLayout);
telefonoTextInputLayout = (TextInputLayout) findViewById(R.id.telefonoTextInputLayout);
femeninoRadioButton = (RadioButton) findViewById(R.id.femeninoRadioButton);
}
public void mostrarMensaje(View view) {
// Snackbar.make(view, R.string.mensaje, Snackbar.LENGTH_LONG).show();
String nombre = nombreEditText.getText().toString();
String correo = emailEditText.getText().toString();
String telefono = telefonoEditText.getText().toString();
// VALIDACIONES
nombreTextInputLayout.setError(null);
emailTextInputLayout.setError(null);
telefonoTextInputLayout.setError(null);
if (esNombreValido(nombre)) {
if (esCorreoValido(correo)) {
if (esTelefonoValido(telefono)) {
Toast.makeText(getApplicationContext(), "Todos los datos correctos", Toast.LENGTH_SHORT).show();
if(femeninoRadioButton.isChecked()) {
Toast.makeText(getApplicationContext(), "ES MUJER", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "ES HOMBRE", Toast.LENGTH_SHORT).show();
}
} else {
telefonoTextInputLayout.setError("El teléfono no es válido");
}
} else {
emailTextInputLayout.setError("El correo no es válido");
}
} else {
nombreTextInputLayout.setError("El nombre no es válido");
}
}
private boolean esNombreValido(String nombre) {
Pattern patron = Pattern.compile("^[a-zA-Z ]+$");
return patron.matcher(nombre).matches();
}
public boolean esTelefonoValido(String telefono) {
return Patterns.PHONE.matcher(telefono).matches();
}
public boolean esCorreoValido(String correo) {
return Patterns.EMAIL_ADDRESS.matcher(correo).matches();
}
}
<resources>
<string name="app_name">Formulario</string>
<string name="mensaje">Este mensaje se mostrará</string>
<string name="nombre">Nombre</string>
<string name="correo_electronico">Correo electrónico</string>
<string name="telefono">Teléfono</string>
<string name="femenino">Femenino</string>
<string name="masculino">Masculino</string>
<string-array name="meses">
<item>Enero</item>
<item>Febrero</item>
<item>Marzo</item>
<item>Abril</item>
</string-array>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment