Optimize Java code

1

Well, I'm creating an alarm in android and I wanted to know if you inves to make a switch for each editText if it was a way to check all in one switch, I tried with a for and I was incremented, but without success, here this is my code, I hope you can help me. PS: I also accept tips to optimize my code.

private EditText edit1, edit2, edit3;
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);
    edit1 = (EditText) findViewById(R.id.edit1);
    edit2 = (EditText) findViewById(R.id.edit2);
    edit3 = (EditText) findViewById(R.id.edit3);
    verificarEdits();
}
void verificarEdits(){
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edit1();
        }
        void edit1() {
            switch (edit1.getText().length()) {
                case 0:
                    Toast.makeText(activity2.this, "Falta preencher a mensagem que deseja no alarme", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    edit2();
            }
        }
        void edit2(){
            switch (edit2.getText().length()) {
                case 0:
                    Toast.makeText(activity2.this, "Falta preencher as horas para que deseja o alarme.", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    edit3();
            }
        }
        void edit3() {
            switch (edit3.getText().length()) {
                case 0:
                    Toast.makeText(activity2.this, "Falta preencher os minutos para que deseja o alarme.", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    converterValores();
            }
        }
    });
}
void converterValores() {
    String mensagem = (edit1.getText().toString());
    int horas = Integer.parseInt(edit2.getText().toString());
    int minutos = Integer.parseInt(edit3.getText().toString());
    enviarValores(mensagem, horas, minutos);
}
void enviarValores(String message, int hour, int minutes) {
    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
            .putExtra(AlarmClock.EXTRA_MESSAGE, message)
            .putExtra(AlarmClock.EXTRA_HOUR, hour)
            .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

}

    
asked by anonymous 05.11.2017 / 00:21

1 answer

0

You can make the tests all followed with ifs that are very short and legible. You only need to directly test the length of each of the EditText and create the error message if it is 0 :

void verificarEdits(){
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String erro = "";

            if (edit1.getText().length() == 0){
                erro += "Falta preencher a mensagem que deseja no alarme\n";
            }
            if (edit2.getText().length() == 0){
                erro += "Falta preencher as horas para que deseja o alarme.\n";
            }
            if (edit3.getText().length() == 0){
                erro += "Falta preencher os minutos para que deseja o alarme.\n";
            }

            if (erro.equals("")){ //se não apanhou erro até aqui converte
                converterValores();
            }
            else {
                Toast.makeText(activity2.this, erro, Toast.LENGTH_SHORT).show();
            }         
        }
    });
}

As it is, the messages appear for each field that is empty. If you have the 3 empty Toast with 3 error messages. If you prefer to only appear one at a time from the first to the last you only need to change the if from edit2 and edit3 to else if .

Since you are converting user-entered text to integer, it may fail if you have letters or other symbols, so it is advisable to capture the exception that represents this failure:

void converterValores() {
    String mensagem = (edit1.getText().toString());

    try {
        int horas = Integer.parseInt(edit2.getText().toString());
        int minutos = Integer.parseInt(edit3.getText().toString());
        enviarValores(mensagem, horas, minutos);
    }
    catch(NumberFormatException e){
        Toast.makeText(this,"As horas e os minutos tem de ser valores númericos",Toast.LENGTH_SHORT).show();
    }
}
    
05.11.2017 / 00:49