Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active January 11, 2024 14:37
Show Gist options
  • Save alvareztech/a31916865cb32aa79cc940383bf5ae05 to your computer and use it in GitHub Desktop.
Save alvareztech/a31916865cb32aa79cc940383bf5ae05 to your computer and use it in GitHub Desktop.
Aplicación Formulario (primera parte) del Curso de desarrollo de aplicaciones Android
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.danielalvarez.formulario.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="200dp"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="mostrarSnackbar"
android:text="@string/mostrarMensaje" />
</RelativeLayout>
<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="abrirFormularrio"
android:src="@mipmap/ic_add_white_36dp" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.danielalvarez.formulario.SegundaActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/nombreTextInputLayout"
android:layout_width="match_parent"
android:layout_height="72dp">
<EditText
android:id="@+id/nombreEditText"
android:layout_width="match_parent"
android:layout_height="36dp"
android:hint="@string/nombre" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/sexo" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<RadioButton
android:id="@+id/masculinoRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/masculino" />
<RadioButton
android:id="@+id/femeninoRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/femenino" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@string/intereses" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<CheckBox
android:id="@+id/javaCheckBox"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:text="@string/java" />
<CheckBox
android:id="@+id/swiftCheckBox"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:text="@string/swift" />
<CheckBox
android:id="@+id/csharpCheckBox"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:text="@string/csharp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="@+id/correoTextInputLayout"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1">
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="@string/correo" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/telefonoTextInputLayout"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1">
<EditText
android:id="@+id/telefonoEditText"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="@string/telefono" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@string/enviarCorreos" />
<Switch
android:id="@+id/correosSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:onClick="enviarFormulario"
android:text="@string/enviar" />
</LinearLayout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.danielalvarez.formulario"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
}
package com.danielalvarez.formulario;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mostrarSnackbar(View view) {
Snackbar.make(view, R.string.hola, Snackbar.LENGTH_LONG).show();
}
public void abrirFormularrio(View view) {
Intent intent = new Intent(this, SegundaActivity.class);
startActivity(intent);
}
}
package com.danielalvarez.formulario;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Switch;
import java.util.regex.Pattern;
public class SegundaActivity extends AppCompatActivity {
private EditText nombreEditText;
private EditText correoEditText;
private EditText telefonoEditText;
private TextInputLayout nombreTextInputLayout;
private TextInputLayout correoTextInputLayout;
private TextInputLayout telefonoTextInputLayout;
private RadioButton masculinoRadioButton;
private RadioButton femeninoRadioButton;
private CheckBox javaCheckBox;
private CheckBox swiftCheckBox;
private CheckBox csharpCheckBox;
private Switch correosSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_segunda);
nombreEditText = (EditText) findViewById(R.id.nombreEditText);
correoEditText = (EditText) findViewById(R.id.emailEditText);
telefonoEditText = (EditText) findViewById(R.id.telefonoEditText);
nombreTextInputLayout = (TextInputLayout) findViewById(R.id.nombreTextInputLayout);
correoTextInputLayout = (TextInputLayout) findViewById(R.id.correoTextInputLayout);
telefonoTextInputLayout = (TextInputLayout) findViewById(R.id.telefonoTextInputLayout);
masculinoRadioButton = (RadioButton) findViewById(R.id.masculinoRadioButton);
femeninoRadioButton = (RadioButton) findViewById(R.id.femeninoRadioButton);
javaCheckBox = (CheckBox) findViewById(R.id.javaCheckBox);
swiftCheckBox = (CheckBox) findViewById(R.id.swiftCheckBox);
csharpCheckBox = (CheckBox) findViewById(R.id.csharpCheckBox);
correosSwitch = (Switch) findViewById(R.id.correosSwitch);
}
public void enviarFormulario(View view) {
String nombre = nombreEditText.getText().toString();
String correo = correoEditText.getText().toString();
String telefono = telefonoEditText.getText().toString();
if (esNombreValido(nombre)) {
if (esCorreoValido(correo)) {
if (esTelefonoValido(telefono)) {
Log.i("MIFOR", nombre);
Log.i("MIFOR", correo);
Log.i("MIFOR", telefono);
if (masculinoRadioButton.isChecked()) {
Log.i("MIFOR", "Es masculino");
} else {
Log.i("MIFOR", "Es femenino");
}
Log.i("MIFOR", "Intereses");
if (javaCheckBox.isChecked()) {
Log.i("MIFOR", " Java");
}
if (swiftCheckBox.isChecked()) {
Log.i("MIFOR", " Swift");
}
if (csharpCheckBox.isChecked()) {
Log.i("MIFOR", " CSharp");
}
if (correosSwitch.isChecked()) {
Log.i("MIFOR", "Si enviar correos");
} else {
Log.i("MIFOR", "No enviar correos");
}
} else {
telefonoTextInputLayout.setError(getString(R.string.telefono_invalido));
}
} else {
correoTextInputLayout.setError(getString(R.string.correo_invalido));
}
} else {
nombreTextInputLayout.setError(getString(R.string.nombre_invalido));
// Toast.makeText(getApplicationContext(), R.string.informacion_invalida, Toast.LENGTH_LONG).show();
}
}
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment