onTextChanged当我删除值totaly

问题描述:

我试图建立一个提示计算。 这是我的代码部分:onTextChanged当我删除值totaly

public void onTextChanged(CharSequence s, int start, int before, int count) { 

    double billAmount = Double.parseDouble(s.toString()); 
    tip10EditText.setText("" + billAmount * .1); 
    tip15EditText.setText("" + billAmount * .15); 
    tip20EditText.setText("" + billAmount * .2); 
    total10EditText.setText("" + (billAmount + billAmount * 0.1)); 
    total15EditText.setText("" + (billAmount + billAmount * 0.15)); 
    total20EditText.setText("" + (billAmount + billAmount * 0.2)); 

} 

的问题是,当我删除一切从账单编辑文本,应用程序崩溃.. 我的朋友找到了一些办法来对付它。这里是他的代码:

public void onTextChanged(CharSequence s, int start, int before, int count) { 

    if (!s.toString().equals("")) { 
     bill = Double.parseDouble(s.toString()); 

     tip10EditText.setText((bill/10) + ""); 
     total10EditText.setText((bill + (bill/10)) + ""); 

     // the String.format("%.2f", is to SHORTEN the tail of the double to 2 digits 
     tip15EditText.setText(String.format("%.2f", (bill/6.6666))); 
     total15EditText.setText(String.format("%.2f",(bill + (bill/6.6666)))); 
     tip20EditText.setText(String.format("%.2f", (bill/5))); 
     total20EditText.setText(String.format("%.2f",(bill + (bill/5)))); 

     // to handle the seekBar changing according to the bill 
     double billCu = Double.parseDouble(billEditText.getText().toString()); 
     // total20EditText.setText(String.format("%.2f",(tipOf)); 
     double progressPer=Double.parseDouble(customTipTextView.getText().toString()); 
     double tipOf = (billCu/100)*progressPer; 
     tipCustomEditText.setText(String.format("%.2f",(tipOf))); 
     totalCustomEditText.setText(String.format("%.2f",((billCu+tipOf)))); 
    } 
    else { 
     tip10EditText.setText(""); 
     total10EditText.setText(""); 
     tip15EditText.setText(""); 
     total15EditText.setText(""); 
     tip20EditText.setText(""); 
     total20EditText.setText(""); 
     tipCustomEditText.setText(""); 
     totalCustomEditText.setText("");    
    } 
} 

有没有更好的方法来处理这个问题?

+0

什么都会做ü如果输入的文本字符? –

+0

除了检查它是否为空(使用's.length> 0'或'TextUtils.isEmpty(s)'),您可能会发现'TextUtils.isDigitsOnly(s)'有用,或者使用'try-catch '块来处理无效输入。 –

+0

@Evgency Golding如果你找到一个有用的答案upvote或接受答案,这将有利于观众了解解决方案。 –

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    double billAmount = 0.0; 
    if(s.length() != 0){ 
     billAmount = Double.parseDouble(s.toString()); 
    } 
     tip10EditText.setText("" + billAmount * .1); 
     tip15EditText.setText("" + billAmount * .15); 
     tip20EditText.setText("" + billAmount * .2); 
     total10EditText.setText("" + (billAmount + billAmount * 0.1)); 
     total15EditText.setText("" + (billAmount + billAmount * 0.15)); 
     total20EditText.setText("" + (billAmount + billAmount * 0.2)); 
} 

上面的代码计算应有的价值只有在任何输入出现在文本框,否则显示为0。

+0

nop ..不起作用 –

试试这个:

 if (!TextUtils.isEmpty(s)) { 

    Then do your stuff.... 

} 

把你的代码在以下if语句:

if (s.length() > 0) 

使用尝试捕捉与catch块NumberFormatException异常,并通过使用该块U可以处理输入的错误类型也。

 try{ 
      //your code 
     }catch(NumberFormatException npe){ 
      //handle the exception and intimate the user if needed. 
     }catch(Exception e){ 

     } 
+0

Tnx!帮助! –