[英]Verify if EditText is empty Android Studio
我有點卡在這種情況下,不知道該怎么辦。 如果用戶不將值插入EditText
,我需要它返回true
或false
。 實際上應用程序崩潰並關閉。
campoBNow = findViewById(R.id.txtMenorBNow);
campoLucro = findViewById(R.id.txtLucro);
btnClicou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pega o texto do BuyNow e Lucro e coloca nas variáveis de texto.
String pegaBNow = campoBNow.getText().toString();
String pegaLucro = campoLucro.getText().toString();
String item = spNivel.getSelectedItem().toString(); //Atribui o ComboBox para String...
//Atribui o valor dos textos convertidos pra float nas variáveis float.
double bNow = Double.parseDouble(pegaBNow);
double lDes = Double.parseDouble(pegaLucro);
/*
Condicional de verificação vazio...
*/
if(TextUtils.isEmpty(pegaBNow)){
tv5.setText("DIGITE UM VALOR DE BUY NOW"); //BuyNow vazio, mostra mensagem...
//Toast.makeText(getApplicationContext(), "DIGITE UM VALOR DE BUY NOW", Toast.LENGTH_LONG ).show();
} else if(TextUtils.isEmpty(pegaLucro)){
tv5.setText("DIGITE UM VALOR DE LUCRO"); //Lucro vazio, mostra mensagem...
//Toast.makeText(getApplicationContext(), "DIGITE UM VALOR DE LUCRO", Toast.LENGTH_LONG ).show();
} else if(TextUtils.isEmpty(pegaBNow) && TextUtils.isEmpty(pegaLucro)){
//Toast.makeText(getApplicationContext(), "DIGITE OS VALORES", Toast.LENGTH_LONG ).show();
}else{
//Atribui o valor dos textos convertidos pra float nas variáveis float.
double res = ((bNow - (0.05*bNow)) - lDes); //Calcula o resultado...
if(res < 0){
tv5.setText("PROPORÇÃO LUCRO E BUY NOW INCORRETO");
Toast.makeText(getApplicationContext(), "PROPORÇÃO INCOMPATÍVEL!", Toast.LENGTH_LONG).show();
}else {
//Início do IF para o nível da carta...
if (item == "Ouro") {
if (res > 0 && res <= 5000) { //Começar a condicional de comparação de valor.
tv5.setText("O RISCO DO TRADE É BAIXO");
} else if (res > 5000 && res <= 15000) {
tv5.setText("O RISCO DO TRADE É MÉDIO");
} else {
tv5.setText("O RISCO DO TRADE É ALTO");
}
} else if (item == "Prata") {
if (res > 0 && res <= 2000) {
tv5.setText("O RISCO DO TRADE É BAIXO");
} else if (res > 2000 && res <= 5000) {
tv5.setText("O RISCO DO TRADE É MÉDIO");
} else {
tv5.setText("O RISCO DO TRADE É ALTO");
}
} else { //else para Bronze.
if (res > 0 && res <= 1000) {
tv5.setText("O RISCO DO TRADE É BAIXO");
} else if (res > 1000 && res <= 3000) {
tv5.setText("O RISCO DO TRADE É MÉDIO");
} else {
tv5.setText("O RISCO DO TRADE É ALTO");
}
}
//Fim do IF para o nível da carta...
}
//tv4.setText("COMPRE O JOGADOR POR ATÉ: " + res + " COINS");
//tv5.setText("RISCO");
}
}
});
移動線條
double bNow = Double.parseDouble(pegaBNow);
double lDes = Double.parseDouble(pegaLucro);
到最后的其他部分(在檢查字符串空條件后工作)
trying to get double value before checking
。
如果為空,則parseDouble()
將拋出NumberFormatException
,這是應用程序崩潰的原因。
使用“for”循環。
private boolean validate(EditText[] fields){
for(int i = 0; i < fields.length; i++){
EditText edtTxtName= fields[i];
if(edtTxtName.getText().toString().length() <= 0){
return false;
}
}
return true;
}
並使用這樣的方法:
boolean checkField= validate(new EditText[] { campoBNow, campoLucro })
如果所有字段都不為空,將返回 true。
我認為您的應用程序崩潰是因為 parseDouble() 無法解析空語句(如果文本框中沒有任何內容)。 您將在調試模式下看到它。
在 parseDouble() 之前檢查文本框中的值:
if (pegaBNow <= 0 or pegaLucro <= 0) {
'Emtpy Text -> do sth. but no Parse
} else {
double bNow = parseDouble(pegaBNow);
double lDes = parseDouble(pegaLucro);
}
此外,還要檢查用戶是否輸入了有效的數字。
TextUtils.isEmpty(variable_name)
應該可以工作,如果應用程序仍然崩潰,您可以使用調試來了解崩潰的原因和位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.