0

I need help on my code.

I have an editText where the user enters the opening and closing time of the store and there is a checkbox with "Close".

The user enters the opening time 09:00 and then with the Text Watcher I add a space and a dash and the user enters the closing time 19:00, so that at the end the time is 09:00 - 19:00.

For example, if the store is closed on Saturday, the user must click on the checkbox and when the checkbox is clicked, the editText has the text set and it says "Closed".

The problem is that when I use the Text Watcher, if the length is equal to 6, it adds the hyphen and then when the user clicks on the checkbox instead of writing "Closed" it says "Closed-".

How can I remove that dash?

There is a condition where the Text Watcher turns off when there are only letters in the editText.

Does anyone have a solution to my problem, do you have any advice for me?

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

        lunedi_inizio_uno = (EditText)findViewById(R.id.editText_uno_lunedi_inizio);

        lunediUno = (CheckBox)findViewById(R.id.checkBox_uno_lunedi);

        lunedi_inizio_uno.addTextChangedListener(new TextWatcher() {

                    int keyDell;

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                        lunedi_inizio_uno.setOnKeyListener(new View.OnKeyListener() {
                            @Override
                            public boolean onKey(View v, int keyCode, KeyEvent event) {

                                if (keyCode == KeyEvent.KEYCODE_DEL)
                                    keyDell = 1;
                                int prevL = 0;
                                return false;
                            }
                        });

                        if (keyDell == 0) {
                            int len = lunedi_inizio_uno.getText().length();

                            if(len == 5) {
                                lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
                                lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
                            }if(len == 6) {
                                lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + "-");
                                lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
                            }if(len == 7) {
                                lunedi_inizio_uno.setText(lunedi_inizio_uno.getText() + " ");
                                lunedi_inizio_uno.setSelection(lunedi_inizio_uno.getText().length());
                            }
                        } else {
                            keyDell = 0;
                        }

                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        // TODO Auto-generated method stub
                    }
                });

                lunediUno.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (((CheckBox) v).isChecked()) {
                                    lunedi_inizio_uno.setText("Chiuso");

                                }


                            }
                        });

                    }
Marie Hopkin
  • 61
  • 1
  • 7

1 Answers1

0

I solved the problem, it was enough to add "-" in the length equal to 5 and eliminate the conditions of length equal to 6 and 7.

Marie Hopkin
  • 61
  • 1
  • 7