如何将整数输出到 EditText 视图?

huangapple go评论73阅读模式
英文:

How do I output an integer to an EditText view?

问题

我有一个输入为EditText的部分我尝试将其用作输出我尝试了以下内容

FoodIncomeCounter.setText(TotalFood.getText().toString());

FoodIncomeCounter = Integer.parseInt(TotalFood.getText());

String FoodIncomeCounter = String.valueOf(TotalFood);


但是都没有起作用。对于第一和第二个选项,“getText()”无法解析。我是否能够将输出显示在EditText视图中,还是需要创建一个单独的TextView并在其中显示?希望你能理解我说的。我的目标是能够使用FoodCampX和FoodUpgradeX变量来计算收入,并将其输出到FoodIncomeCounter变量/EditText视图(目前可以手动输入)。FoodIncomeCounter是一个EditText视图,FoodCampX、FoodUpgradeX和FoodIncome都是整数,TotalFood是一个整数。感谢您的教导。

以下是代码:

// 从用户输入获取并转换为变量形式
FoodIncomeCounter = (EditText) findViewById(R.id.FoodIncomeCounter);
IncomeSubmitButton = (Button) findViewById(R.id.IncomeSubmitButton);

FoodCamp1Counter = (EditText) findViewById(R.id.FoodCamp1Counter);
FoodCamp2Counter = (EditText) findViewById(R.id.FoodCamp2Counter);
FoodCamp3Counter = (EditText) findViewById(R.id.FoodCamp3Counter);
FoodUpgrade1Counter = (EditText) findViewById(R.id.FoodUpgrade1Counter);
FoodUpgrade2Counter = (EditText) findViewById(R.id.FoodUpgrade2Counter);
FoodUpgrade3Counter = (EditText) findViewById(R.id.FoodUpgrade3Counter);
FoodCampSubmitButton = (Button) findViewById(R.id.FoodCampSubmitButton);

}

//按钮
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.IncomeSubmitButton:
            //重置为初始值
            FoodIncomeCounter.setText("");

            //接收输入的值
            FoodIncome = Integer.parseInt(FoodIncomeCounter.getText().toString());
            break;
        case R.id.FoodCampSubmitButton:
            //重置为初始值
            FoodCamp1Counter.setText("");
            FoodCamp2Counter.setText("");
            FoodCamp3Counter.setText("");
            FoodUpgrade1Counter.setText("");
            FoodUpgrade2Counter.setText("");
            FoodUpgrade3Counter.setText("");

            //接收输入的值
            FoodCamp1 = Integer.parseInt(FoodCamp1Counter.getText().toString());
            FoodCamp2 = Integer.parseInt(FoodCamp2Counter.getText().toString());
            FoodCamp3 = Integer.parseInt(FoodCamp3Counter.getText().toString());
            FoodUpgrade1 = Integer.parseInt(FoodUpgrade1Counter.getText().toString());
            FoodUpgrade2 = Integer.parseInt(FoodUpgrade2Counter.getText().toString());
            FoodUpgrade3 = Integer.parseInt(FoodUpgrade3Counter.getText().toString());

            //获取食物收入并显示
            TotalFood = FoodCamp1 + (FoodCamp2 * 2) + (FoodCamp3 * 3) + (FoodUpgrade1 * 2) + (FoodUpgrade2 * 4) + (FoodUpgrade3 * 6);

            //以下是我尝试过但不起作用的三个选项
            FoodIncomeCounter.setText(String.valueOf(TotalFood));
            FoodIncomeCounter.setText(Integer.toString(TotalFood));
            FoodIncomeCounter.setText(String.valueOf(TotalFood));
            //------------------------------------------------------
            break;
        default:
            break;
    }
}
英文:

I have an EditText as input. I am trying to use it for output as well. I've tried the following:

FoodIncomeCounter.setText(TotalFood.getText().toString());

FoodIncomeCounter = Integer.parseInt(TotalFood.getText());

String FoodIncomeCounter = String.valueOf(TotalFood);

and nothing works. For the 1st and 2nd option the "getText()" cannot be resolved. Am I able to output to an EditText view, or do I need to make a separate TextView and output to that? Hopefully that all makes sense to you. My goal is to be able to use the FoodCampX and FoodUpgradeX variables to calculate the income and output that into FoodIncomeCounter variable/EditText view (which currently you can manually input). FoodIncomeCounter is an EditText view, FoodCampX and FoodUpgradeX and FoodIncome are integers, TotalFood is an integer. Thank you for teaching me.

Here is the code:

//to get from user input and into variable form
  
    FoodIncomeCounter = (EditText) findViewById(R.id.FoodIncomeCounter);
    IncomeSubmitButton = (Button) findViewById(R.id.IncomeSubmitButton);

    FoodCamp1Counter = (EditText) findViewById(R.id.FoodCamp1Counter);
    FoodCamp2Counter = (EditText) findViewById(R.id.FoodCamp2Counter);
    FoodCamp3Counter = (EditText) findViewById(R.id.FoodCamp3Counter);
    FoodUpgrade1Counter = (EditText) findViewById(R.id.FoodUpgrade1Counter);
    FoodUpgrade2Counter = (EditText) findViewById(R.id.FoodUpgrade2Counter);
    FoodUpgrade3Counter = (EditText) findViewById(R.id.FoodUpgrade3Counter);
    FoodCampSubmitButton = (Button) findViewById(R.id.FoodCampSubmitButton);

}

    //Buttons
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.IncomeSubmitButton:
                //reset to value
                FoodIncomeCounter.setText("");

                //receive the inputted values
                FoodIncome = Integer.parseInt(FoodIncomeCounter.getText().toString());
                break;
            case R.id.FoodCampSubmitButton:
                //reset to value
                FoodCamp1Counter.setText("");
                FoodCamp2Counter.setText("");
                FoodCamp3Counter.setText("");
                FoodUpgrade1Counter.setText("");
                FoodUpgrade2Counter.setText("");
                FoodUpgrade3Counter.setText("");

                //receive the inputted values
                FoodCamp1 = Integer.parseInt(FoodCamp1Counter.getText().toString());
                FoodCamp2 = Integer.parseInt(FoodCamp2Counter.getText().toString());
                FoodCamp3 = Integer.parseInt(FoodCamp3Counter.getText().toString());
                FoodUpgrade1 = Integer.parseInt(FoodUpgrade1Counter.getText().toString());
                FoodUpgrade2 = Integer.parseInt(FoodUpgrade2Counter.getText().toString());
                FoodUpgrade3 = Integer.parseInt(FoodUpgrade3Counter.getText().toString());

                //get food income and show
                TotalFood = FoodCamp1 + (FoodCamp2 * 2) + (FoodCamp3 * 3) + (FoodUpgrade1 * 2) + (FoodUpgrade2 * 4) + (FoodUpgrade3 * 6);

                //These 3 options are what iv tried and do not work
                FoodIncomeCounter.setText(TotalFood.getText().toString());
                FoodIncomeCounter = Integer.parseInt(TotalFood.getText());
                String FoodIncomeCounter = String.valueOf(TotalFood);
                //------------------------------------------------------
                break;
            default:
                break;
        }
    }

答案1

得分: 1

基于对你的字段类型的猜测,以下代码应该有效,因为TotalFood很可能是一个数字:

FoodIncomeCounter.setText(String.valueOf(TotalFood));

getText()EditText上的一个方法,但它不能用于数字。

英文:

Based on some educated guesses on the types of your fields, the following should work, as TotalFood is probably a number:

FoodIncomeCounter.setText(String.valueOf(TotalFood));

getText() is a method on EditTexts but it doesn't work on numbers.

答案2

得分: 1

FoodIncomeCounter.setText(TotalFood.getText().toString());

改为

FoodIncomeCounter.setText(String.valueOf(TotalFood));

因为getText()是用于EditTextTextView等组件的方法,而不是用于数据类型的方法。

英文:

Change

FoodIncomeCounter.setText(TotalFood.getText().toString());

to

FoodIncomeCounter.setText(String.valueOf(TotalFood));

As getText() is a method on components like EditText, TextView, and not datatypes.

huangapple
  • 本文由 发表于 2020年10月11日 23:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定