[英]how to get text from textview
if I have set text in textview in such way, which is not problem:如果我以这种方式在 textview 中设置文本,这不是问题:
tv.setText("" + ANS[i]);
this simply getting from this way.这只是从这种方式得到的。
String a = tv.getText().toString();
int A = Integer.parseInt(a);
But in case of setting value in textView.但是在 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. 我没有测试过这个 - 但它应该让你对你需要采取的方向有一个大概的了解。
For this to work, I'm going to assume a few things about the text of the TextView
: 为了实现这一点,我将假设一些关于TextView
文本的内容:
TextView
consists of lines delimited with "\\n"
. TextView
由以"\\n"
分隔的行组成。 TextView
which will all include one operator and one number. 在第一行之后, TextView
可以有可变数量的行,这些行将包括一个运算符和一个数字。 Char
of a line. 操作员将始终是一条线的第一个Char
。 First we get the text: 首先我们得到文字:
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 );
}
The method calculate should look like this, assuming that we know the first Char
of a line is the operator: 方法计算应该如下所示,假设我们知道一行的第一个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() );
}
EDIT 编辑
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 );
The method getValue
is a recursive method and it should look like this: 方法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 );
}
Special cases that the recursive method does not handle: 递归方法无法处理的特殊情况:
Also the fact the we're using Integers
might give some "odd" results in some cases as eg 5/3 = 1. 此外,我们使用Integers
的事实可能会在某些情况下给出一些“奇数”结果,例如5/3 = 1。
split with the + sign like this way 像这样用+符号分开
String a = tv.getText().toString();
String aa[];
if(a.contains("+"))
aa = a.split("+");
now convert the array 现在转换数组
Integer.parseInt(aa[0]); // and so on
it is me code if you like get your Text View Content to String如果您想将文本视图内容转换为字符串,则是我的代码
TextView TextViewTest = findViewById(R.id.textView);
String Test1 = TextViewTest.getText().toString();
Toast.makeText(this,Test1, Toast.LENGTH_SHORT).show();
试试这样。
tv1.setText(" " + Integer.toString(X[i]) + "\n" + "+" + " " + Integer.toString(Y[i]));
You have to do the following: 您必须执行以下操作:
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;
If it is the sum of all the numbers you want, I made a little snippet for you that can handle both + and - using a regex (I left some print-calls in there to help visualise what happens): 如果它是你想要的所有数字的总和,我为你做了一个可以处理+和 - 使用正则表达式的小片段(我在那里留下了一些打印调用来帮助可视化发生的事情):
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);
This code prints the following: 此代码打印以下内容:
5
- 9
+ 5
5
-9
5
Sum: 1
Edit: sorry if I misunderstood your question, it was a little bit unclear what you wanted to do. 编辑:对不起,如果我误解了你的问题,有点不清楚你想做什么。 I assumed you wanted to get the sum of the numbers as an integer rather than a string. 我假设你想要将数字的总和作为整数而不是字符串。
Edit2: to get the numbers separate from each other, do something like this instead: 编辑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)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.