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);
}
}
}