[英]Store EditText in an array and display in a TextView
I am trying to store my EditTexts in an array and display all of them in a TextView, then have a sum function to get the sum of the array and display the sum. 我试图将我的EditTexts存储在一个数组中,并在TextView中显示所有它们,然后有一个sum函数来获取数组的总和并显示总和。 But I am stuck at this point. 但是我被困在这一点上。
public void Clickme (View view) {
public void clickMe (View v){
//my EditText’s ID is called myInput
EditText myInput = (EditText) findViewById(R.id.myInput);
//my TextView’s ID is called text2
TextView text2 = (TextView) findViewById(R.id.text2);
String str = myInput.getText().toString();
text2.setText(str);
int [] array = new int [myInput.length()];
//I am stuck at this point
for(int i=0; i <myInput.length(); i++) {
array[i] = Integer.valueOf(myInput.getText(i));
}
//sum function
for (int i: array) {
str = str + i;
EditText.setText(str);
}
}
You have a method in a method... That won't compile 您在方法中有一个方法...不会编译
public void Clickme (View view) {
public void clickMe (View v){
And what are you trying to do here? 您想在这里做什么? EditText.setText(str);
setText
is not a static method. setText
不是静态方法。
then have a sum function to get the sum of the array and display the sum 然后有一个sum函数来获取数组的总和并显示总和
And here? 和这里? str = str + i;
That isn't a sum of numbers. 那不是数字的总和。 You are appending a number to a string. 您将数字附加到字符串。
And this method doesn't exist... myInput.getText(i)
而且这种方法不存在... myInput.getText(i)
Based on your loop and array creation, looks like you want to add all the digits of a number. 根据循环和数组的创建,您似乎想添加一个数字的所有数字。
private EditText myInput;
private TextView text2;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(...);
...
myInput = (EditText) findViewById(R.id.myInput);
text2 = (TextView) findViewById(R.id.text2);
}
public void clickMe (View view) {
String input = myInput.getText().toString();
int number = 0;
if (!input.isEmpty()) {
number = Integer.parseInt(input);
}
// Sum the digits in a number
int sum = 0;
while (number > 0) {
sum += number % 10;
number = number / 10;
}
text2.setText(String.valueOf(sum));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.