简体   繁体   English

如果edittext留为空白,则强制为“ 0”

[英]Force “0” if edittext left blank

so I have tried some of the methods in prior posts re this problem yet none of them seem to work for me (user error no doubt). 所以我在以前的帖子中尝试了一些方法来解决这个问题,但是它们似乎都不适合我(毫无疑问,是用户错误)。

Basically my calculation button will crash if a edittext is left blank. 基本上,如果将edittext留为空白,我的计算按钮将崩溃。 What I need to happen is that if it is left blank, the edittext is given a value of 0. 我需要做的是,如果将其保留为空白,则edittext的值为0。

Here is my java: 这是我的java:

public class kCals extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
EditText thirdNumber;
TextView addResult;
Button btnAdd;
double num1,num2,num3,sum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_k_cals);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    {
    firstNumber = (EditText)findViewById(R.id.txtNumber1);
    secondNumber = (EditText)findViewById(R.id.txtNumber2);
    thirdNumber = (EditText) findViewById(R.id.txtNumber3);
    addResult = (TextView)findViewById(R.id.txtResult);
    btnAdd = (Button)findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new View.OnClickListener()

    {public void onClick(View v) {
            num1 = Double.parseDouble(firstNumber.getText().toString());
            num2 = Double.parseDouble(secondNumber.getText().toString());
            num3 = Double.parseDouble(thirdNumber.getText().toString());
            sum = (num1 * 4) + (num2 * 4) + (num3 * 9);
            addResult.setText(String.format("%.2f", sum));}
        });

   }}}

If anyone can help with this that would be great. 如果有人可以帮助您,那就太好了。 Cheers, Jess. 干杯,杰西。

It would be best to have a method do this for you like the following. 最好有一种方法可以像下面这样为您做到这一点。

public double parseDouble(String doubleText) {
    if(TextUtils.isEmpty(doubleText)) {
        return 0;
    }

    try {
        return Double.parseDouble(doubleText);
    } catch(NumberFormatException e) {
        // do something if invalid number maybe also return 0?
        return 0;
    }
}

Then your code becomes 然后你的代码变成

num1 = parseDouble(firstNumber.getText().toString());
num2 = parseDouble(secondNumber.getText().toString());
num3 = parseDouble(thirdNumber.getText().toString());

You could also update the method to pass the EditText objects themselves to avoid all the getText().toString() calls if you wanted to. 您也可以更新该方法以传递EditText对象本身,从而避免所有getText().toString()调用(如果需要)。

How about making a check before u parse. 在解析之前进行检查怎么​​样。

if(firstNumber.getText().toString() != ""){
    num1 = Double.parseDouble(firstNumber.getText().toString());
}
else{
    num1 = 0;
}

Note that this will still output exceptions if you allow entries other than numbers in the EditText. 请注意,如果您允许在EditText中输入数字以外的内容,这仍将输出异常。

You should try using a function to check for the text. 您应该尝试使用函数来检查文本。 For example : 例如 :

double getValue(Editable text){
   if (text == null || text.length() == 0){
     return 0;
} else {
   return Double.parseDouble(text.toString());
}
}

and call it : 并称之为:

num1 = getValue(firstNumber.getText());

It is really basic. 这真的很基础。 Parsing a String to a Double may occur some unexpected, so the try-catch is the best choice for you to solve this question. String解析为Double可能会发生一些意外情况,因此try-catch是解决此问题的最佳选择。

Check with your edittext value length something like String s = edittext.gettext().toString(); 检查您的edittext值长度,例如String s = edittext.gettext()。toString(); If(s.length()!=0) Your stuff If(s.length()!= 0)你的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM