2011-12-15 31 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))); 
    }