2015-12-27 30 views
-1

我有一個BufferedReader buf = new BufferedReader(new FileReader(file)); buf的結果我想在EditText中設置。 我這樣做的:如何從緩衝區讀取器在EditText中設置文本Android

DecimalFormat REAL_FORMATTER = new DecimalFormat("0.##"); 

et202.setText(buf.readLine()); 
et203.setText(String.valueOf(REAL_FORMATTER.format(buf.readLine()))); 
et204.setText(buf.readLine()); 

看來,這是不正確的做法,導致我的應用程序crashs。

但是,當我設置

et203.setText(String.valueOf(REAL_FORMATTER.format(buf.readLine()))); 

et203.setText(buf.readLine()); 

我的應用程序工作正常。有沒有辦法從BuefferedReader中設置格式?

我只想說的EditText上具有以下格式: 「0.00」 - >節目總是2位小數喜歡上了PIC

Picture

+0

您是否想在'EditText'設置有兩個小數點十進制值?你可以分享你的'文件'中有什麼? – Tauqir

+0

我已經設置了'EditText'到'安卓的inputType =「numberDecimal」' 的問題是,在'file'是隻喜歡'5'或'112'或'4472300'裏面的數字。但我的應用程序必須顯示'5.00','112.00','4472300.00'等。 隨着BufferedReader中我不能這樣做。我不知道如何當我使用數字從文件 – noobee

+0

格式化'et203.setText(buf.readLine());'結果是'5'而不是'5.00' 所以我想'et203.setText(將String.valueOf(REAL_FORMATTER.format(buf.readLine())));' 但讓我應用程序崩潰:( – noobee

回答

0

試試這個

BufferedReader reader = new BufferedReader(new FileReader(file));  
String text = reader.readLine().toString(); 
et203.setText(String.format("%.2f", Double.parseDouble(text))); 

或者

String text = reader.readLine().toString(); 
double d = Double.parseDouble(text); 
DecimalFormat f = new DecimalFormat("##.00"); 
et203.setText(f.format(d)+""); 
+0

@noobee,請執行情況和預期產出分享屏幕截圖 – Tauqir

+0

@noobee,我已經更新了我。回答。 – Tauqir