如何从TextView的文本文本、TextView

2023-09-06 14:31:30 作者:世上哪有真情

如果我在TextView中以这样的方式,这不是问题,设置文本:

if I have set text in textview in such way, which is not problem:

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

这仅仅让来自这个样子。

this simply getting from this way.

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

但是,如果在TextView中设置值。

But in case of setting value in textView.

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

这就是这个样子

which is like this

              5
             +9

我有问题,这个值怎么得到的。

I have problem , this value how to get.

推荐答案

我没有测试过这一点 - 但它应该给你,你需要采取的方向,总体思路

I haven't tested this - but it should give you a general idea of the direction you need to take.

对于这项工作,我将承担有关的文本的几件事情的的TextView

For this to work, I'm going to assume a few things about the text of the TextView:

的TextView 由线分隔的\ N。的 在第一行不包括操作符(+, - ,*或/). 第一行后可以有一个可变数目的的TextView 将所有包含一个运营商线路和一个数。 在操作员将百达是第一个字符行。 The TextView consists of lines delimited with "\n". The first line will not include an operator (+, -, * or /). After the first line there can be a variable number of lines in the TextView which will all include one operator and one number. An operator will allways be the first Char of a line.

首先我们得到的文本:

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

然后,我们把它分解为每个行:

Then we split it up for each line:

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

现在,我们需要计算总价值:

Now we need to calculate the total value:

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 );
}

该方法计算应该是这样的,假设我们知道第一字符 A线的运营商:

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() );
}

修改

因此​​,按照上述评论指出下面做左到右的计算,忽略了正常秩序(+和/前+和 - )。

So the above as stated in the comment below does "left-to-right" calculation, ignoring the normal order ( + and / before + and -).

下面做的计算以正确的方式:

The following does the calculation the right way:

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

方法的getValue 是一个递归方法,它应该是这样的:

The method getValue is a recursive method and it should look like this:

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 );
}

特殊情况,该递归方法不处理:

Special cases that the recursive method does not handle:

如果第一数目是负的例如-3 + 5 * 8。 双击运营商如3 * -6或5 / -4。

另外的事实,我们使用整数可能会给一些奇怪的结果在某些情况下,例如5/3 = 1。

Also the fact the we're using Integers might give some "odd" results in some cases as e.g. 5/3 = 1.