2014-10-12 3 views
0

필요한 EditText를 만들려고합니다. 부울로 시도하지만 작동하지 않습니다. 내 EditText에 txtEdat, txtTelefon, txtEmail에만 무언가를 입력해도 setError가 사라지지 않습니다 ... 부울은 txtEmail (마지막 코드)을 인식하기 때문에이 코드에서는 쓸모없는 것처럼 보입니다.필수 EditText Android가 작동하지 않습니다.

boolean contador; 
contador = false; 
//Creem el intent 

Intent iIntent = new Intent(MyActivity.this, FormEnviado.class); 
//Creem un bundle que es on anira la informació que es pasarà a la altre activity 
Bundle bBundle = new Bundle(); 
// si isChecked el valor es True 
bBundle.putBoolean("cbValidator", cbDades.isChecked()); 

// En cada if mirem si hi ha algun camp buit 
bBundle.putString("cognom", txtCognom.getText().toString()); 
if(txtCognom.getText().toString().length()==0) 
{ 
    contador = false; 
    txtCognom.setError("Aquet camp no pot estar buit"); 
} 
else 
{ 
    contador = true; 
} 

bBundle.putString("nom", txtNom.getText().toString()); 

if(txtNom.getText().toString().length()==0) 
{ 
    contador = false; 
    txtNom.setError("Aquet camp no pot estar buit"); 
} 
else 
{ 
    contador = true; 
} 

bBundle.putString("edat", txtEdat.getText().toString()); 
if(txtEdat.getText().toString().length()==0) 
{ 
    contador = false; 
    txtEdat.setError("Aquet camp no pot estar buit"); 
} 
else 
{ 
    contador = true; 
} 

bBundle.putString("telefon", txtTelefon.getText().toString()); 

if(txtTelefon.getText().toString().length()==0) 
{ 
    contador = false; 
    txtTelefon.setError("Aquet camp no pot estar buit"); 
} 
else 
{ 
    contador = true; 
} 

bBundle.putString("email", txtEmail.getText().toString()); 

if(txtEmail.getText().toString().length()==0) 
{ 
    contador = false; 
    txtEmail.setError("Aquet camp no pot estar buit"); 
} 
else 
{ 
    contador = true; 
} 

// Toast i no ens deixarà enviar-lo 
if (!contador) 
{ 
    Toast.makeText(getApplicationContext(), "Has de omplir el formulair per enviar-lo", Toast.LENGTH_SHORT).show(); 
} 

// Formulario omplert 
else 
{ 
    //Afegim la informació de bundle al intent 
    iIntent.putExtras(bBundle); 
    //Inicialitzem el intent 
    startActivity(iIntent); 
} 
+0

그냥 팁 :'iIntent' 대신'intent'를 사용할 수 있습니다. – Simas

+0

나도 잘 압니다 만, 제 생각에는 아무 것도 바뀌지 않는다고 생각합니다 ... –

+0

모든 필드가 필요합니다 (비어 있지 않음)? –

답변

0

코드의 논리가 올바르지 않습니다. 당신은이 같은 네 가지 검사가 : 다른 검사 통과하지 못한 경우에도 (txtEmail을 평가) 마지막 검사를 통과하면

if (required field is empty) { 
    contador = false; 
} else { 
    contador = true 
} 

을 다음 contador는 사실 일 것이다.

contador에서 true으로 초기화하고이 4 가지 검사 모두에서 else 블록을 제거하십시오. 모두 통과하면 contador은 여전히 ​​true입니다.

+0

Uh, my bad .... 당신의 도움에 감사 드리며 마지막으로 수정해야할 방법이 없다는 것은 setatrror가 Edat, Telefon 및 E-mail, Nom 및 Cognom에서만 Pop- 어떤 솔루션? –

+0

'setError (null)'을 호출하면 팝업이 사라집니다. 그거하고 있니? – Karakuri

+0

아니요, 빈 필드가있는 양식을 보내려고하면 팝업이 많이 보입니다. 원하는 것이지만 Edat, Email, Telefon에서만 입력이 시작되면서 제가 입력했는지 궁금합니다. "android : inputType ="textCapWords "and"android : inputType = "textCapSentence"를 사용하십시오. –

관련 문제