android - How to properly trigger a datePicker and validation from an EditText? -
i trying validation of edittext field, allow me elaborate because using bunch of concepts have own questions in stackoverflow have feeling i'm trying not yet answered in of them, , rather basic either i'm screwing badly or found bug, also, many of popular questions , answers couple of years old...
i have 2 edittext fields contain initial date , termination date input datepicker, validating after termination date has been input datepicker date happens after initial date, , if doesn't displays error using seterror.
so far good, manage there bothering me:
when first click on edittext
nothing (because use setinputtype(inputtype.type_null)
along android:focusableintouchmode="true"
in xml; avoid showing me keyboard, clicks, selected, , no keyboard, no datepicker, until click again, onclick method triggers datepicker , works intended.)
i going show images don't have rep points :/
if use setfocusable(false)
, weird happens, when seterror comes icon shows without text.
if don't use setfocusable(false)
, edittext appears selected when activity starts (no cursor flashing because disabled input) , stays selected (it stays blue can click anywhere)
if don't want selected @ start use android:focusableintouchmode="true
" in xml in parent layout, per recommendation of question (stop edittext gaining focus @ activity startup) , solves problem. have first click , nothing happens second click , shows datepicker problem again... doing wrong.
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#ffffff" android:layout_height="match_parent" android:gravity="center_vertical" style="@style/apptheme" android:id="@+id/fechasalariolayout" android:focusableintouchmode="true">
<edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:textalignment="center" android:inputtype="date" android:ems="10" android:gravity="center_horizontal" android:onclick="showdatepickerdialogft" android:id="@+id/fechaterminacioninpt" android:layout_weight="10" android:layout_gravity="center_horizontal" />
public class fechasalario extends actionbaractivity { private button calcularbtn; static edittext fechaingresoinpt, fechaterminacioninpt, salarioinpt; public static boolean isfechainicial, isfechaterminacion = false; private radiogroup sueldoperiodo, salariomin; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //remove title bar this.requestwindowfeature(window.feature_no_title); //remove notification bar this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); //set content view after above sequence (to avoid crash) this.setcontentview(r.layout.fechasalario); calcularbtn = (button) findviewbyid(r.id.calcularbtn); fechaingresoinpt = (edittext) findviewbyid(r.id.fechaingresoinpt); fechaterminacioninpt = (edittext) findviewbyid(r.id.fechaterminacioninpt); salarioinpt = (edittext) findviewbyid(r.id.salarioinpt); sueldoperiodo = (radiogroup)findviewbyid(r.id.sueldoperiodo); salariomin = (radiogroup) findviewbyid(r.id.salariomin); fechaterminacioninpt.setinputtype(inputtype.type_null); //fechaterminacioninpt.setfocusable(false); //onchangetxtlistener fechaterminacioninpt.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // todo auto-generated method stub } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub fechaterminacioninpt.seterror(null); validarfechaterminacion(fechaterminacioninpt, fechaingresoinpt); // pass edittext obj here. } }); } @override public boolean oncreateoptionsmenu(menu menu) { return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { return super.onoptionsitemselected(item); } public void onclickcalcular(view view) { //llamar una funcion calcular que llame un activity para desplegar. // enviar bundle //verificar que los datos hayan sido introducidos correctamente. //verificar fechas y cantidades válidas. //verificar que todos los radio buttons esten seleccionados intent intent = new intent(this, calcular.class); bundle variables = new bundle(); variables = getintent().getextras(); radiobutton sueldoperiodo = (radiobutton)this.findviewbyid(sueldoperiodo.getcheckedradiobuttonid()); radiobutton salariomin = (radiobutton)this.findviewbyid(salariomin.getcheckedradiobuttonid()); //validar fecha inicial menor fecha de terminación variables.putstring("fecha_inicio", string.valueof(fechaingresoinpt.gettext())); variables.putstring("fecha_terminacion", string.valueof(fechaterminacioninpt.gettext())); variables.putstring("salario", string.valueof(salarioinpt.gettext())); variables.putstring("sueldo_periodo", sueldoperiodo.gettext().tostring()); variables.putstring("salario_min", salariomin.gettext().tostring()); intent.putextras(variables); startactivity(intent); } public void validarfechaterminacion(edittext edt, edittext edt2){ datetimeformatter formatter = datetimeformat.forpattern("dd/mm/yyyy"); datetime fechainicio = formatter.parsedatetime(string.valueof(fechaingresoinpt.gettext())); datetime fechaterm = formatter.parsedatetime(string.valueof(fechaterminacioninpt.gettext())); if (fechainicio.isafter(fechaterm)){ /* fechaterminacioninpt.requestfocus();*/ fechaterminacioninpt.seterror("la fecha de terminación ocurre antes que la fecha de inicio"); } else{ // nothing fechaterminacioninpt.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus){ fechaterminacioninpt.seterror(null); } }); } //validar cantidad es un numero monetario válido //validar cantidad es un numero entero válido // validar radio buttons han sido seleccionados. } public void showdatepickerdialogfi(view v) { isfechainicial = true; isfechaterminacion = false; dialogfragment newfragment = new datepickerfragment(); newfragment.show(getfragmentmanager(), "datepicker"); } public void showdatepickerdialogft(view v) { isfechaterminacion = true; isfechainicial = false; dialogfragment newfragment = new datepickerfragment(); newfragment.show(getfragmentmanager(), "datepicker"); } }
i have similar "problem" concerning error message , focusability. if textview not focusable message isn't shown. suppose how works e.g. if have many fields validated , validation error in each of them, message shown textview contains cursor.
in app i'm using time , date picker way do. have textviews validated not focusable. layout looks this:
<linearlayout android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_margintop="10dp" android:background="@drawable/box_bg" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingright="13dp" android:text="@string/start_time" /> <edittext android:id="@+id/edittext_start_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginright="10dp" android:layout_weight="1" android:focusable="false" android:inputtype="none" android:onclick="showtimepicker" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="10dp" android:paddingright="13dp" android:text="@string/end_time" /> <edittext android:id="@+id/edittext_end_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginright="10dp" android:layout_weight="1" android:focusable="false" android:inputtype="none" android:onclick="showtimepicker" /> </linearlayout>
maybe solve problem textviews getting focus automatically. if not, let know , figure out.
[edit] here code snippet i'm using:
<tablerow android:id="@+id/tablerow" android:layout_marginbottom="3dp" android:layout_margintop="3dp" android:background="@drawable/box_bg" android:baselinealigned="false"> <textview android:id="@+id/text_view_birthday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_gravity="center_vertical" android:layout_span="1" android:text="@string/birthday" /> <edittext android:id="@+id/text_birthday" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="1" android:layout_gravity="center_vertical" android:cursorvisible="false" android:editable="false" android:focusable="false" android:focusableintouchmode="false" android:inputtype="date|textnosuggestions" android:layout_weight="1" android:layout_span="2" /> </tablerow>
Comments
Post a Comment