2011-12-15 30 views
28

獲取文本,如果我在這樣的方式設置TextView的文本,這不是問題:如何從TextView的

tv.setText("" + ANS[i]); 

這只是從這種方式獲得。

 String a = tv.getText().toString(); 
    int A = Integer.parseInt(a); 

但是在textView中設置值的情況下。

tv1.setText(" " + X[i] + "\n" + "+" + " " + Y[i]); 

是這樣

   5 
      +9 

我有問題,這個值怎麼弄。

+0

你想顯示除了這些數字的結果..? – Mudassir 2011-12-15 09:27:32

+1

有什麼問題? – freshDroid 2011-12-15 09:28:00

+0

我的問題是,我在一個textview中有5 + 9的值,因爲我已經提到過,所以如何獲得像tv.getText()。toString()這樣的值。 – 2011-12-15 09:33:41

回答

34

我的天堂」 t測試了這個 - 但它應該給你一個總體的想法你需要採取的方向。

對於這項工作,我將承擔有關TextView文本的幾件事情:

  1. TextView包括與"\n"分隔線。
  2. 第一行不包含運算符(+, - ,*或/)。
  3. 第一行後,有可在TextView線的可變數量,這將所有包括一個操作者和一個數。
  4. 運營商將是一條線路的第一個Char

首先我們得到的文本:

String input = tv1.getText().toString(); 

然後我們把它分解爲每個行:

String[] lines = input.split("\n"); 

現在我們需要計算出總價值:

int total = Integer.parseInt(lines[0].trim()); //We know this is a number. 

for(int i = 1; i < lines.length(); i++) { 
    total = calculate(lines[i].trim(), total); 
} 

計算方法應該是這樣的,假設我們知道第一個一行的是運營商:

private int calculate(String input, int total) { 
    switch(input.charAt(0)) 
     case '+': 
     return total + Integer.parseInt(input.substring(1, input.length()); 
     case '-': 
     return total - Integer.parseInt(input.substring(1, input.length());    
     case '*': 
     return total * Integer.parseInt(input.substring(1, input.length());    
     case '/': 
     return total/Integer.parseInt(input.substring(1, input.length()); 
} 

EDIT

因此,作爲在註釋中所述的上述下面做「左到右」的計算,忽略了正常順序(+和/之前和 - )。

下做計算的正確方法:

String input = tv1.getText().toString(); 
input = input.replace("\n", ""); 
input = input.replace(" ", ""); 
int total = getValue(input); 

方法getValue是一個遞歸方法,它應該是這樣的:

private int getValue(String line) { 
    int value = 0; 

    if(line.contains("+")) { 
    String[] lines = line.split("\\+"); 
    value += getValue(lines[0]); 

    for(int i = 1; i < lines.length; i++) 
     value += getValue(lines[i]); 

    return value; 
    } 

    if(line.contains("-")) { 
    String[] lines = line.split("\\-"); 
    value += getValue(lines[0]); 

    for(int i = 1; i < lines.length; i++) 
     value -= getValue(lines[i]); 

    return value; 
    } 

    if(line.contains("*")) { 
    String[] lines = line.split("\\*"); 
    value += getValue(lines[0]); 

    for(int i = 1; i < lines.length; i++) 
     value *= getValue(lines[i]); 

    return value; 
    } 

    if(line.contains("/")) { 
    String[] lines = line.split("\\/"); 
    value += getValue(lines[0]); 

    for(int i = 1; i < lines.length; i++) 
     value /= getValue(lines[i]); 

    return value; 
    } 

    return Integer.parseInt(line); 
} 

特殊情況,該遞歸方法不處理:

  • 如果第一個數字是負的例如-3 + 5 * 8。
  • 雙操作員例如3 * -6或5/-4。

另外我們使用的事實Integers可能會在某些情況下給出一些「奇怪」的結果,例如, 5/3 = 1.

0

試試這個。

tv1.setText(" " + Integer.toString(X[i]) + "\n" + "+" + " " + Integer.toString(Y[i])); 
0

分裂與+符號像這樣

String a = tv.getText().toString(); 
String aa[]; 
if(a.contains("+")) 
    aa = a.split("+"); 

現在轉換陣列

Integer.parseInt(aa[0]); // and so on 
0

你必須做到以下幾點:

a=a.replace("\n"," "); 
a=a.trim(); 
String b[]=a.split("+"); 
int k=Integer.ValueOf(b[0]); 
int l=Integer.ValueOf(b[1]); 
int sum=k+l; 
0

如果它是你想要的所有數字的總和,我爲你提供了一個可以處理+和 - 使用正則表達式的小片段(我留下了一些打印電話以幫助想象會發生什麼):

final String string = " " + 5 + "\n" + "-" + " " + 9+"\n"+"+"+" "+5; //Or get the value from a TextView 
    final Pattern pattern = Pattern.compile("(-?).?(\\d+)"); 
    Matcher matcher = pattern.matcher(string); 

    System.out.print(string); 
    System.out.print('\n'); 
    int sum = 0; 

    while(matcher.find()){ 
     System.out.print(matcher.group(1)); 
     System.out.print(matcher.group(2)); 
     System.out.print('\n'); 
     sum += Integer.parseInt(matcher.group(1)+matcher.group(2)); 
    } 
    System.out.print("\nSum: "+sum); 

此代碼打印如下:

5 
- 9 
+ 5 
5 
-9 
5 

Sum: 1 

編輯:對不起,如果我誤解了你的問題,這是一個有點不清楚你想要做什麼。我假設你想要將整數作爲整數而不是字符串。

EDIT2:要想讓數字彼此分開,做這樣的事情,而不是:

final String string = " " + 5 + "\n" + "-" + " " + 9+"\n"+"+"+" "+5; //Or get the value from a TextView 
    final Pattern pattern = Pattern.compile("(-?).?(\\d+)"); 
    Matcher matcher = pattern.matcher(string); 
    ArrayList<Integer> numbers = new ArrayList<Integer>(); 

    while(matcher.find()){ 
     numbers.add(Integer.parseInt(matcher.group(1)+matcher.group(2))); 
    }