Tesis 1.0.0
Loading...
Searching...
No Matches
ContactFragment.java
Go to the documentation of this file.
1package com.example.food_front;
2import android.content.Intent;
3import android.net.Uri;
4import android.os.Bundle;
5import android.view.LayoutInflater;
6import android.view.View;
7import android.view.ViewGroup;
8import android.widget.Button;
9import android.widget.EditText;
10import android.widget.Toast;
11
12import androidx.annotation.NonNull;
13import androidx.annotation.Nullable;
14import androidx.fragment.app.Fragment;
15
16public class ContactFragment extends Fragment {
17
18 private EditText etNombre, etApellido, etEmail, etMensaje;
19 private Button btnEnviar;
20
21 @Nullable
22 @Override
23 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
24 View view = inflater.inflate(R.layout.fragment_contact, container, false);
25
26 // Inicializar vistas
27 etNombre = view.findViewById(R.id.etNombre);
28 etApellido = view.findViewById(R.id.etApellido);
29 etEmail = view.findViewById(R.id.etEmail);
30 etMensaje = view.findViewById(R.id.etMensaje);
31 btnEnviar = view.findViewById(R.id.btnEnviar);
32
33 // Agregar listener al botón de Enviar
34 btnEnviar.setOnClickListener(new View.OnClickListener() {
35 @Override
36 public void onClick(View v) {
37 // Implementar la lógica para enviar el mensaje
38 enviarMensaje();
39 }
40 });
41
42 return view;
43 }
44
45 private void enviarMensaje() {
46 String nombre = etNombre.getText().toString().trim();
47 String apellido = etApellido.getText().toString().trim();
48 String email = etEmail.getText().toString().trim();
49 String mensaje = etMensaje.getText().toString().trim();
50 String regex = "^[a-zA-Z\\s]+$"; // Permite solo letras, números y espacios
51 String emailPattern = "[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}";
52 if (nombre.isEmpty() || apellido.isEmpty() || email.isEmpty() || mensaje.isEmpty()) {
53 Toast.makeText(getContext(), "Por favor completa todos los campos", Toast.LENGTH_SHORT).show();
54 return;
55 }
56 ;
57 if (!nombre.matches(regex)) {
58 // Mostrar un mensaje de error al usuario
59 Toast.makeText(getContext(), "El campo nombre no es válido (no debe contener números)", Toast.LENGTH_SHORT).show();
60 return;
61 }
62 if (nombre.length() <3 || nombre.length() > 15) {
63 Toast.makeText(getContext(), "El nombre debe tener más de 3 y hasta 15 caracteres", Toast.LENGTH_SHORT).show();
64 return;
65 }
66 if (!apellido.matches(regex)) {
67 // Mostrar un mensaje de error al usuario
68 Toast.makeText(getContext(), "El campo apellido no es válido (no debe contener números)", Toast.LENGTH_SHORT).show();
69 return;
70 }
71 if (apellido.length() < 3 || apellido.length() > 15) {
72 Toast.makeText(getContext(), "El apellido debe tener más de 3 y hasta 15 caracteres", Toast.LENGTH_SHORT).show();
73 return;
74 }
75 if (!email.matches(emailPattern)) {
76 Toast.makeText(getContext(), "El email no es válido", Toast.LENGTH_SHORT).show();
77 return;
78 }
79 if (mensaje.length() < 5 || mensaje.length() > 100) {
80 Toast.makeText(getContext(), "El mensaje debe contener de 5 a 100 carateres", Toast.LENGTH_SHORT).show();
81 return;
82 }
83 if (!mensaje.matches(regex)) {
84 // Mostrar un mensaje de error al usuario
85 Toast.makeText(getContext(), "El texto del mensaje no es válido", Toast.LENGTH_SHORT).show();
86 return;
87 }
88
89 // Crear el intent para enviar el email
90 Intent emailIntent = new Intent(Intent.ACTION_SEND);
91 emailIntent.setData(Uri.parse("mailto:"));
92 emailIntent.setType("text/plain");
93 emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
94 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Mensaje de la app");
95 emailIntent.putExtra(Intent.EXTRA_TEXT, "Nombre: " + nombre + "\nApellido: " + apellido + "\nMensaje: " + mensaje);
96
97 // Verificar si hay una aplicación de email instalada
98 if (emailIntent.resolveActivity(getContext().getPackageManager()) != null) {
99 startActivity(emailIntent);
100 Toast.makeText(getContext(), "Mensaje enviado", Toast.LENGTH_SHORT).show();
101 } else {
102 Toast.makeText(getContext(), "No se encontró una aplicación de email instalada", Toast.LENGTH_SHORT).show();
103 }
104 }
105
106}
View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)