Reputation: 371
Is there any way to predefine a value for strings in order not to have the error when any of the fields are empty? All porcentagem 1, 2 and 3 are optional, so it's not the case to ask the user to input some data, but predefine values in order not to have the values. Beginner question.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cpc_inicial = (EditText) findViewById(R.id.cpc_inicial);
porcentagem1 = (EditText) findViewById(R.id.porcentagem1);
porcentagem2 = (EditText) findViewById(R.id.porcentagem2);
porcentagem3 = (EditText) findViewById(R.id.porcentagem3);
cpc_final = (TextView) findViewById(R.id.cpc_final);
botao1 = (Button) findViewById(R.id.botao1);
cpc_inicial.setInputType(InputType.TYPE_CLASS_NUMBER);
porcentagem1.setInputType(InputType.TYPE_CLASS_NUMBER);
porcentagem2.setInputType(InputType.TYPE_CLASS_NUMBER);
porcentagem3.setInputType(InputType.TYPE_CLASS_NUMBER);
botao1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(porcentagem3 != null ) {
float cpc = Float.parseFloat(cpc_inicial.getText().toString());
float v1 = Float.parseFloat(porcentagem1.getText().toString());
float v2 = Float.parseFloat(porcentagem2.getText().toString());
float v3 = Float.parseFloat(porcentagem3.getText().toString());
TextView cpcfinal = cpc_final;
if(cpc > 0.0 && v1 != 0.0 && v2 != 0.0 && v3 != 0.0 )
{
soma = (cpc*v1/100)+cpc;
soma = soma*(v2/100)+soma;
soma = soma*(v3/100)+soma;
String sum = Float.toString(soma);
cpcfinal.setText(sum);
}
} else
{
TextView cpcfinal = cpc_final;
soma = 0;
cpcfinal.setText("ops!"); }
}
});
}
Thanks
Upvotes: 0
Views: 88
Reputation: 944
Every time a form is submitted, you should check whether each field has a proper value. For example, if you want to check weather an optional field has a value or not, you should do something like this:
String optionalText = optionalFieldName.getText().toString();
if (optionalText.equals("some expected value")) {
//Do something with the value here.
}
Of course, you would need to do something similar for every optional field, and really should also do the inverse for fields that are not option to be safe, and perhaps warn the user that the field is required, for example:
String text = fieldName.getText().toString();
if (text.equals("")) {
//field is empty, so warn the user that it is required.
}
If the value you are looking for should be numerical in nature, then you should do something like this:
String text = field.getText().toString();
if (!text.equals("")) {
//Field has at least some text in it.
try {
float val = Float.parseFloat(text);
}catch (NumberFormatException ex) {
//Enterered text was not a float value, so you should do something
// here to let the user know that their input was invalid and what you expect
}
//Do something with the value
}
Upvotes: 2
Reputation: 3785
Either add the values to your xml layout using the android:text="..."
attribute or use TextUtils.isEmpty(...)
to detect if the string is empty and assign a default value yourself.
Upvotes: 1