2017-02-28 18 views
-1

如果我用editText字段作爲Double值,我得到錯誤:我得到的錯誤,如果我使用EDITTEXT作爲雙

public void Format1(View view) { 
    DecimalFormat num = new DecimalFormat("000.00"); 

    EditText editText = (EditText)findViewById(R.id.editText_075_1); 
    TextView textView = (TextView)findViewById(R.id.textView_075_1); 

    //textView.setText(num.format(1.6789));// works 

    textView.setText(num.format(Double.parseDouble(editText.toString()))); // error 
+0

可以請你分享確切的錯誤信息! –

+0

引起:java.lang.NumberFormatException:無效的double:「android.support.v7.widget.AppCompatEditText {8322571 VFED..CL。.F ...... 416,64-776,256#7f1001f2 app:id/editText_075_1 }「 – Testiest

+0

你給的輸入文字是什麼/ –

回答

0

這意味着,在EDITTEXT不包含正確的兩倍!因此,如果在將文本設置爲textView之前檢查編輯文本是否爲正確的數字格式,那麼效果會更好!

boolean checkDouble(String val) { 
    try { 
     Double.parseDouble(val); 
     return true; 
    } catch (NumberFormatException e) { 
     return false; 
    } 
} 

而且使用它像

if(checkDouble(editText.toString())){ 
    textView.setText(num.format(Double.parseDouble(val))); 
}else{ 
    textView.setText("Please enter a valid double!"); 
} 

希望這有助於!

+0

我得到」請輸入一個有效的雙精度!「 這意味着此代碼在此行不是正確的: textView.setText(num.format(Double.parseDouble(val))); – Testiest

+0

你的輸入是什麼!? –

0

問題修正: 我們使用流,無串 vergot(gettext的):

editText.getText().toString()) // String : correct 

editText.getText()的toString())//流

相關問題