从TextView中获取文本的方法

41
如果我已经以这种方式在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

我有问题,如何获取这个值。


你想展示这些数字相加的结果吗? - Mudassir
我的问题是,我在一个TextView中有5+9的值,正如我之前提到的那样。那么如何获取这个值,类似于tv.getText().toString(); - Horrorgoogle
tv.getText().toString() 不起作用吗? - Jave
@jave 他需要将其转换为整数,因此他不希望只有数字而没有任何其他字符。 - Padma Kumar
好的,我更新了我的答案,告诉你如何获取所有数字的列表,而不仅仅是总和。 - Jave
6个回答

59

我没有测试过这个代码,但是它应该可以给你提供需要采取的方向。

为了使此代码生效,我将假设一些关于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 );
}

假设我们已知一行的第一个 Char 是运算符,那么方法 calculate 应该像这样:

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

编辑

根据下面的评论所述,上面的代码执行“从左到右”的计算,而忽略了正常的顺序(加号和除号在加减号之前计算)。

下面的代码按正确的方式进行计算:

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。

另外,我们使用整数可能会在某些情况下产生“奇怪”的结果,例如5/3=1。


这样做效果不佳。考虑以下情况:5 + 5 * 3 实际结果是20(5 + 5 * 3 = 5 + 15 = 20),但您的代码严格按照从左到右的顺序操作,忽略了乘法/除法应该先处理的事实,并给出30(5 + 5 * 3 = 10 * 3 = 30)。 - Jave
这就是为什么我说:“但它应该给你一个大致的方向。” - kaspermoerch
是的,只是指出来,这样别人就不会因为它没有按照他们的预期工作而感到困惑(正如你所说,你还没有测试过它)。 - Jave

3

如果您想将文本视图内容转换为字符串,以下是我的代码:

    TextView TextViewTest = findViewById(R.id.textView);
    String Test1 = TextViewTest.getText().toString();
    Toast.makeText(this,Test1, Toast.LENGTH_SHORT).show();

3

这里介绍一种用加号进行字符串分割的方法

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

现在将数组转换

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

String a = tv.getText().toString();,这个我不理解。 - Horrorgoogle
你需要将"\n"替换为"",然后修剪字符串,然后拆分字符串。 - Pratik

0

试试这样。

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

他想从TextView中获取值,而不是将值设置到TextView中。他已经将值设置到TextView中了。 - Pratik

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

编辑:如果我误解了你的问题,那么很抱歉,因为你想要做什么有点不清楚。我假设你想要将数字的总和作为整数而不是字符串。

编辑2:要将数字分开,请改用以下方法:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接