java - 将 EditText 存储在数组中并显示在 TextView 中

标签 java android arrays

我试图将我的 EditTexts 存储在一个数组中并将它们全部显示在 TextView 中,然后使用 sum 函数来获取数组的总和并显示总和。但我现在陷入困境。

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

最佳答案

你的方法中有一个方法......它不会编译

public void Clickme (View view) {
   public void clickMe (View v){

你想在这里做什么? EditText.setText(str);

setText 不是静态方法。

then have a sum function to get the sum of the array and display the sum

这里呢? str = str + i;

这不是数字的总和。您正在将一个数字附加到一个字符串中。

这个方法不存在... myInput.getText(i)

<小时/>

根据您的循环和数组创建,看起来您想要添加数字的所有数字。

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

关于java - 将 EditText 存储在数组中并显示在 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600960/

相关文章:

java - 连接到有效 IP 端口时抛出 UnknownHostException

java - 使用 JUnit 和 Mockito 测试局部变量

python - 数组中的条件选择

mysql - 从数据库(rails)中的数组中提取数据

android - WebView : webpage not available but I load it from an html string

python - 使用结构化数组来命名 numpy 数组中的轴

java - 将 MyObject 的页面映射到 MyObjectDTO 的页面

java - java Socket编程中从服务器发送文件列表到客户端 - PrintWriter无法发送?

android - 如何在Android中实现Side Sheet Dialog

java - 从 firebase 在 auth OTP 中获取错误的应用程序名称