Last active
July 12, 2016 04:40
-
-
Save alvareztech/ca1f4fc2893e60d3f2e156df237c54cf to your computer and use it in GitHub Desktop.
Aplicación de ejemplo de uso de Pickers y Dialogs del Curso de desarrollo de aplicaciones Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="tech.alvarez.ejemplopickerdialogs.MainActivity"> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:onClick="abrirPickerHora" | |
android:text="@string/hora" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:onClick="abrirPickerFecha" | |
android:text="@string/fecha" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:onClick="abrirMensaje" | |
android:text="@string/dialogo_mensaje" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:onClick="abrirItems" | |
android:text="@string/dialogo_items" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:onClick="abrirSeleccion" | |
android:text="@string/dialogo_seleccion" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string-array name="colores"> | |
<item>Rojo</item> | |
<item>Verde</item> | |
<item>Morado</item> | |
</string-array> | |
<string-array name="lenguajes"> | |
<item>Java</item> | |
<item>C++</item> | |
<item>Swift</item> | |
</string-array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// build.gradle del módulo | |
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 24 | |
buildToolsVersion "24.0.0" | |
defaultConfig { | |
applicationId "tech.alvarez.ejemplopickerdialogs" | |
minSdkVersion 15 | |
targetSdkVersion 24 | |
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:24.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.ejemplopickerdialogs; | |
import android.app.DatePickerDialog; | |
import android.app.Dialog; | |
import android.app.TimePickerDialog; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.format.DateFormat; | |
import android.view.View; | |
import android.widget.DatePicker; | |
import android.widget.TimePicker; | |
import android.widget.Toast; | |
import java.util.Calendar; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void abrirPickerHora(View view) { | |
TimePickerFragment timePickerFragment = new TimePickerFragment(); | |
timePickerFragment.show(getSupportFragmentManager(), "timePicker"); | |
} | |
public void abrirPickerFecha(View view) { | |
DatePickerFragment datePickerFragment = new DatePickerFragment(); | |
datePickerFragment.show(getSupportFragmentManager(), "datePicker"); | |
} | |
public void abrirMensaje(View view) { | |
MiDialogFragment miDialogFragment = new MiDialogFragment(); | |
miDialogFragment.show(getSupportFragmentManager(), "memsaje"); | |
} | |
public void abrirItems(View view) { | |
MiItemsDialogFragment miItemsDialogFragment = new MiItemsDialogFragment(); | |
miItemsDialogFragment.show(getSupportFragmentManager(), "items"); | |
} | |
public void abrirSeleccion(View view) { | |
MiSelectorDialogFragment miSelectorDialogFragment = new MiSelectorDialogFragment(); | |
miSelectorDialogFragment.show(getSupportFragmentManager(), "multi"); | |
} | |
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
final Calendar fechaActual = Calendar.getInstance(); | |
int hora = fechaActual.get(Calendar.HOUR_OF_DAY); | |
int minuto = fechaActual.get(Calendar.MINUTE); | |
return new TimePickerDialog(getActivity(), this, hora, minuto, DateFormat.is24HourFormat(getActivity())); | |
} | |
public void onTimeSet(TimePicker view, int hora, int minuto) { | |
// cuando se selecciona la hora | |
Toast.makeText(getActivity(), "Se selecciono " + hora + ":" + minuto, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
final Calendar fechaActual = Calendar.getInstance(); | |
int anio = fechaActual.get(Calendar.YEAR); | |
int mes = fechaActual.get(Calendar.MONTH); | |
int dia = fechaActual.get(Calendar.DAY_OF_MONTH); | |
return new DatePickerDialog(getActivity(), this, anio, mes, dia); | |
} | |
public void onDateSet(DatePicker view, int anio, int mes, int dia) { | |
// cuando se selecciona la fecha | |
Toast.makeText(getActivity(), "Fecha: " + dia + "/" + mes + "/" + anio, Toast.LENGTH_LONG).show(); | |
} | |
} | |
public static class MiDialogFragment extends DialogFragment { | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle(R.string.titulo); | |
builder.setMessage(R.string.mensaje); | |
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
Toast.makeText(getActivity(), "Acepto!!", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
Toast.makeText(getActivity(), "Cancelo!!", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
builder.setNeutralButton(R.string.neutral, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
Toast.makeText(getActivity(), "nEUTRO!!", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
return builder.create(); | |
} | |
} | |
public static class MiItemsDialogFragment extends DialogFragment { | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle(R.string.titulo); | |
builder.setItems(R.array.colores, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
Toast.makeText(getActivity(), "Item " + i, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
return builder.create(); | |
} | |
} | |
public static class MiSelectorDialogFragment extends DialogFragment { | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle(R.string.titulo); | |
builder.setMultiChoiceItems(R.array.lenguajes, null, new DialogInterface.OnMultiChoiceClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i, boolean b) { | |
// cuando se selecciona item posición i con estado b | |
Toast.makeText(getActivity(), "Item " + i + " > " + b, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
} | |
}); | |
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
} | |
}); | |
return builder.create(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">EjemploPickerDialogs</string> | |
<string name="hora">Hora</string> | |
<string name="fecha">Fecha</string> | |
<string name="titulo">Título</string> | |
<string name="mensaje">Un mensaje</string> | |
<string name="neutral">Neutro</string> | |
<string name="dialogo_mensaje">Diálogo Mensaje</string> | |
<string name="dialogo_items">Diálogo Items</string> | |
<string name="dialogo_seleccion">Diálogo Selección</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment