如何从文本视图中获取文本 [英] how to get text from textview
问题描述
如果我以这种方式在 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] + "
" + "+" + " " + Y[i]);
是这样的
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
由" "
分隔的行组成.- 第一行将不包含运算符(+、-、* 或/).
- 在第一行之后,
TextView
中可以有可变数量的行,其中都包含 one 运算符和 one 数字. - 操作符总是一行的第一个
Char
.
- The
TextView
consists of lines delimited with" "
. - 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( "
" );
现在我们需要计算总值:
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 );
}
方法calculate应该是这样的,假设我们知道一行的第一个Char
是操作符:
The method calculate should look like this, assuming that we know the first Char
of a line is the operator:
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( "
", "" );
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.
此外,我们使用 Integers
在某些情况下可能会产生一些奇怪"的结果,例如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.
这篇关于如何从文本视图中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!