Optimize Java code

Asked

Viewed 136 times

1

Good, I am creating an alarm on android e I wanted to know if inves to have to make a switch for each editText se Avia way to check all on a single switch, I tried with a for and was incremented, but unsuccessful, here 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);
    }
}

}

1 answer

0

You can do the tests all in a row with ifs which are very short and readable. You only need to directly test the length of each of the EditText and create the error message if 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();
            }         
        }
    });
}

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

Since you are converting text written by the user to an integer, it may fail if you have letters or other symbols and so it is advisable to capture the exception representing 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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.