I need you to force me to change my TextView

0

I have a TextView that I fill with DatePickerDialog and TimePickerDialog , I also have to default by automatically adding the date and time of the day on which We are, because here is my problem. I have since I need to fill TextView to move forward, if you leave blank you get a message asking you to fill TextView but as I said above, by default you add the hour and < em> date therefore it is never empty.

How can I do to recognize my TextView as empty when the time and date

are added by default?

Or putting it another way, that does not recognize that it is filled with the automatic text

Here my code: (The class is not complete, only what I think it takes to try to solve my problem)

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);

            editTextFecha = (TextView) findViewById(R.id.editTextFecha);

            Calendar calendario = Calendar.getInstance();
            dia = calendario.get(Calendar.DAY_OF_MONTH);
            mes = calendario.get(Calendar.MONTH);
            ano = calendario.get(Calendar.YEAR);
            hora = calendario.get(Calendar.HOUR_OF_DAY);
            minutos = calendario.get(Calendar.MINUTE);
            mostrarFecha();

            selectorFecha = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    dia = dayOfMonth;
                    mes = month;
                    ano = year;
                    mostrarFecha();
                    mostrarHora();
                }
            };

        }

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
                case 0:
                    return new DatePickerDialog(this, selectorFecha, ano, mes, dia);
            }
            return null;
        }

        public void mostrarCalendario(View control) {
            showDialog(TIPO_DIALOGO);
        }


        public void mostrarFecha() {
            editTextFecha.setText(dia + "/" + (mes + 1) + "/" + ano + " " + hora + ":" + String.format("%02d", minutos));

        }

        private void mostrarHora() {
            TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {
                            hora = hourOfDay;
                            minutos = minute;
                            mostrarFecha();
                        }
                    }, hora, minutos, true);
            timePickerDialog.show();
        }

        // aqui compruebo que los campos estén rellenos, en este caso 'Fecha' es lo que estamos mirando

                btnguardar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (verificarCampoNombre() && verificarCampoFecha()
                        && verificarCampoZodiaco()) {
                    if (estadoEditarPersona()) {
                        editarPersona();
                    } else {
                        try {
                            abc_id = (int)insertarNuevoPersona();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                    finish();
                } else {
                    if (editTextNombre.getText().toString().equals("")) {
                        mensaje.mostrarMensajeCorto(getResources().getString(R.string.intro_nombre));
                    }
                    if (editTextFecha.getText().toString().equals("")) {
                        mensaje.mostrarMensajeCorto(getResources().getString(R.string.intro_fecha));
                    }
                    if (editTextZodiaco.getText().toString().equals("")) {
                        mensaje.mostrarMensajeCorto(getResources().getString(R.string.intro_zodiaco));
                    }
                }
            }
        });

 private boolean verificarCampoFecha() {
        if (editTextFecha.getText().toString().equals("")) {
            return false;
        }
        return true;
    }

        }
    }

Is it possible? Or I have to remove the date and time

    
asked by UserNameYo 18.02.2017 в 14:47
source

2 answers

1

After having seen several details of your code I have come to the conclusion that your problem, rather than code, is the conception of your application.

If you want to do something contradictory: "make something that has data is recognized as something that does not have it" means that there is an error in the way you are conceiving your program.

That does not mean that you can not do what you ask in the question, but if you solve the contradiction that I refer to you by analyzing again how the app should work, you will save yourself a lot of headaches now and for the future.

Many times, it is true, we have errors that we could call "physical", such as Null Pointer Exception or others, but in many cases these errors occur because the logic we are following for our program is somewhat tortuous.

Sometimes there are very easy solutions for things that seem complicated, and when our code seems to be too complicated, it is convenient to turn back because almost certainly there is a logical error , a < strong> design error or another that makes difficult solutions that, corrected that error, are very simple to implement.

    
answered by 06.03.2017 / 17:11
source
0

What I can think of is that you use the hint property (which is a background text, the one that deals regularly with the forms but is not actually stored in its .text property in this case textview, then you could fill that hint with the automatic date and although it will be displayed in the textview at first it will not store it as text, in text value, it will still have value "", or the one that you want to put as value when starting your program

private TextView text2hint;




     text2hint=(TextView) findViewById(R.id.textViewProbarhint);
     text2hint.setText("");

     text2hint.setHint("FECHA");

Or for your method something like this

 public void mostrarFecha() {
            editTextFecha.setHint(dia + "/" + (mes + 1) + "/" + ano + " " + hora + ":" + String.format("%02d", minutos));

        }
    
answered by 18.02.2017 в 19:51