如果我在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
我有问题,这个值如何得到。
我还没有测试过这个,但它应该给你一个你需要采取的方向的大致想法。
为了使其工作,我将假设关于TextView
的文本的一些事情:
-
TextView
由"n"
分隔的线组成。 - 第一行不包含操作符(+,-,*或/)。
- 在第一行之后,
TextView
中可以有可变数量的行,这些行都将包含一个操作符和一个数字。 - 操作符总是一行的第一个
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 );
}
方法calculate应该是这样的,假设我们知道一行的第一个Char
是操作符:
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。
事实上,我们使用Integers
可能会在某些情况下给出一些"奇怪"的结果,例如5/3 = 1。
这是我的代码,如果你想让你的文本视图内容字符串
TextView TextViewTest = findViewById(R.id.textView);
String Test1 = TextViewTest.getText().toString();
Toast.makeText(this,Test1, Toast.LENGTH_SHORT).show();
像这样用+号分开
String a = tv.getText().toString();
String aa[];
if(a.contains("+"))
aa = a.split("+");
现在转换数组
Integer.parseInt(aa[0]); // and so on
试一下
tv1.setText(" " + Integer.toString(X[i]) + "n" + "+" + " " + Integer.toString(Y[i]));
您必须这样做:
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;
如果它是您想要的所有数字的总和,我为您制作了一个小片段,可以使用正则表达式处理+和-(我在那里留下了一些打印调用,以帮助可视化发生的事情):
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)));
}