如何将整数输出到 EditText 视图?
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 variable/EditText 视图(目前您可以手动输入)。 FoodIncomeCounter 是一个EditText 视图,FoodCampX 和FoodUpgradeX 以及FoodIncome 是整数,TotalFood 是整数。谢谢你教我。
代码如下:
//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;
}
}
根据对您的字段类型的一些有根据的猜测,以下应该有效,因为 TotalFood
可能是一个数字:
FoodIncomeCounter.setText(String.valueOf(TotalFood));
getText()
是 EditText
上的一种方法,但它不适用于数字。
改变
FoodIncomeCounter.setText(TotalFood.getText().toString());
至
FoodIncomeCounter.setText(String.valueOf(TotalFood));
因为 getText() 是针对 EditText、TextView 等组件而非数据类型的方法。
我有一个 EditText 作为输入。我也在尝试将它用于输出。我尝试了以下方法:
FoodIncomeCounter.setText(TotalFood.getText().toString());
FoodIncomeCounter = Integer.parseInt(TotalFood.getText());
String FoodIncomeCounter = String.valueOf(TotalFood);
但没有任何效果。对于第一个和第二个选项,“getText()”无法解析。我可以输出到 EditText 视图,还是需要制作一个单独的 TextView 并输出到它?希望这一切对你有意义。我的目标是能够使用 FoodCampX 和 FoodUpgradeX 变量来计算收入并将其输出到 FoodIncomeCounter variable/EditText 视图(目前您可以手动输入)。 FoodIncomeCounter 是一个EditText 视图,FoodCampX 和FoodUpgradeX 以及FoodIncome 是整数,TotalFood 是整数。谢谢你教我。
代码如下:
//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;
}
}
根据对您的字段类型的一些有根据的猜测,以下应该有效,因为 TotalFood
可能是一个数字:
FoodIncomeCounter.setText(String.valueOf(TotalFood));
getText()
是 EditText
上的一种方法,但它不适用于数字。
改变
FoodIncomeCounter.setText(TotalFood.getText().toString());
至
FoodIncomeCounter.setText(String.valueOf(TotalFood));
因为 getText() 是针对 EditText、TextView 等组件而非数据类型的方法。