Amigos boa tarde sou novo na programação e estou criando um app, preciso utilizar um método boolean para checar o preenchimento do nome dos times para só assim autorizar a entrada dos valores. Fiz a construção mas quando chamo o método public Void golTimeA(View v) não está dando certo. Alguém poderiam me ajudar?
/**
* método para checar preenchimento do nome dos times
*/
private boolean checarPreenchimentoTimes() {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (tA.trim().isEmpty() || tB.trim().isEmpty()) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}
return true;
}
/**
* metodo de chamada da ação dos botões do time A
*/
/**
* Metodo para Adicionar gol ao Time A
*/
public void golTimeA(View v) {
checarPreenchimentoTimes();
golTimeA = golTimeA + 1;
displayTimeA(golTimeA);
}
o seu método checarPreenchimentoTimes() deveria apenas chegar, quem deve tomar a decisão do que fazer caso seja true ou false é em outro lugar. Refatorando, ficaria assim:
/**
* método para checar preenchimento do nome dos times
*/
private boolean checarPreenchimentoTimes(String tA, String tB) {
if (tA.trim().isEmpty() || tB.trim().isEmpty()) {
return false;
}
return true;
}
public void golTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}
golTimeA = golTimeA + 1;
displayTimeA(golTimeA);
}
isso daria na mesma também, porém achei que poderia ficar complicado pra vc entender
/**
* método para checar preenchimento do nome dos times
*/
private boolean checarPreenchimentoTimes(String tA, String tB) {
return tA.trim().isEmpty() || tB.trim().isEmpty();
}
igor_ks
Muito Obrigado amigo!! muito bom funcionou muito bem obrigado pela ajuda.
vou postar aqui como ficou a chamada dos métodos. você acha que da para melhorar? ou ficou bom?
private boolean checarPreenchimentoTimes(String tA, String tB) {
if (tA.trim().isEmpty() || tB.trim().isEmpty()) {
return false;
}
return true;
}
/**
* metodo de chamada da ação dos botões do time A
*/
/**
* Metodo para Adicionar gol ao Time A
*/
public void golTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
golTimeA = golTimeA + 1;
displayTimeA(golTimeA);
}
}
/**
* Metodo para Adicionar falta ao Time A
*/
public void faltaTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
faltaTimeA = faltaTimeA + 1;
displayFaltaTimeA(faltaTimeA);
}
}
/**
* Metodo para Adicionar Cartão Amarelo ao Time A
*/
public void cartaoAmareloTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
cartaoAmareloTimeA = cartaoAmareloTimeA + 1;
displayCartaoAmareloTimeA(cartaoAmareloTimeA);
}
}
/**
* Metodo para Adicionar Cartão Vermelho ao Time A
*/
public void cartaoVermelhoTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
cartaoVermelhoTimeA = cartaoVermelhoTimeA + 1;
displayCartaoVermelhoTimeA(cartaoVermelhoTimeA);
}
}
/**
* Metodo para Adicionar Escanteio ao Time A
*/
public void escanteioTimeA(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
escanteioTimeA = escanteioTimeA + 1;
displayEscanteioTimeA(escanteioTimeA);
}
}
/**
* metodo de chamada da ação dos botões do time B
*/
/**
* Metodo para Adicionar gol ao Time B
*/
public void golTimeB(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
golTimeB = golTimeB + 1;
displayTimeB(golTimeB);
}
}
/**
* Metodo para Adicionar falta ao Time B
*/
public void faltaTimeB(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
faltaTimeB = faltaTimeB + 1;
displayFaltaTimeB(faltaTimeB);
}
}
/**
* Metodo para Adicionar Cartão Amarelo ao Time B
*/
public void cartaoAmareloTimeB(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
cartaoAmareloTimeB = cartaoAmareloTimeB + 1;
displayCartaoAmareloTimeB(cartaoAmareloTimeB);
}
}
/**
* Metodo para Adicionar Cartão Vermelho ao Time B
*/
public void cartaoVermelhoTimeB(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
cartaoVermelhoTimeB = cartaoVermelhoTimeB + 1;
displayCartaoVermelhoTimeB(cartaoVermelhoTimeB);
}
}
/**
* Metodo para Adicionar Escanteio ao Time B
*/
public void escanteioTimeB(View v) {
String tA = TimeA.getText().toString();
String tB = TimeB.getText().toString();
if (!checarPreenchimentoTimes(tA, tB)) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage("DIGITE O NOME DOS TIMES");
dlg.setNeutralButton("OK", null);
dlg.show();
}else {
escanteioTimeB = escanteioTimeB + 1;
displayEscanteioTimeB(escanteioTimeB);
}