我想验证空字段然后执行简单计算。但我的代码不起作用。
我的代码:
public void onClick(View v)
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
if(etValue.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Value", Toast.LENGTH_LONG).show();
}
else{
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes")))
{
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
}
答案 0 :(得分:1)
试试这个..
if(etValue.getText().toString().trim().equals("") || etValue.getText().toString().trim().length() < 0){
Toast.makeText(getApplicationContext(), "Please Enter Value", Toast.LENGTH_LONG).show();
}else{
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes"))){
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
答案 1 :(得分:0)
试试这个...........
public void onClick(View v)
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
String check = String.valueOf(value);
if(check.length < 0){
Toast.makeText(getApplicationContext(), "Please Enter Value", Toast.LENGTH_LONG).show();
return;
}
else{
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes")))
{
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
}
答案 2 :(得分:0)
试试这段代码:
public void onClick(View v)
{
if(etValue.getText().toString().trim().length() == 0) {
Toast.makeText(getApplicationContext(), "Please Enter Value", Toast.LENGTH_LONG).show();
}
else {
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes")))
{
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
}
答案 3 :(得分:0)
public void onClick(View v)
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
String yourValue=etValue.getText().toString();
if(yourValue.equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Value", Toast.LENGTH_LONG).show();
}
else{
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes")))
{
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
}
答案 4 :(得分:0)
使用 TextUtil 类的android
public void onClick(View v)
{
if(TextUtils.isEmpty(etValue.getText().toString()))
etValue.setError("Enter Value");
}
else {
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
if((spBitsBytesFrom.getSelectedItem().toString().equals("Bits")) && (spBitsBytesTo.getSelectedItem().toString().equals("Bytes")))
{
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
}