エディットテキストのあるアプリケーションがあります。すでにデフォルト値は1です。次に、ユーザーは3に変更したいものに変更できます。次に、スピナーから食品の値を選択できます。すべての食品には対応するカロリーがあります。
たとえば、8カロリーのパンを選び、編集テキスト2に入力します。合計カロリーは16カロリーである必要があります。8*2.これまでに試したことをご覧ください。
String[] classes = {
"Cornbread",
"French Bread",
"French Toast",
"French Toast, low fat",
"Italian Bread",
"Wheat Bread",
"Wheat Bread, low calories",
"Wheat Bread, whole wheat"
};
String mealname = selected;
String serving = calories.getText().toString();
int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));
String servng = String.valueOf(i);
int amount = Integer.valueOf(etAmount.getText().toString());
int answer = amount * i;
String strAnswer = String.valueOf(answer);
calories.setText(strAnswer);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sdf.format(new Date());
if ( ( mealname.isEmpty() || servng.isEmpty() ) ){
// call for custom toast
viewErrorToast();
}public void onItemSelected(AdapterView<?> parent, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
selected = parent.getItemAtPosition(position).toString();
switch( position ){
case 0:
strCalories = "188 calories";
calories.setText(strCalories);
break;
case 1:
strCalories = "185 calories";
calories.setText(strCalories);
break;
case 2:
strCalories = "126 calories";
calories.setText(strCalories);
break;
case 3:
strCalories = "149 calories";
calories.setText(strCalories);
break;
case 4:
strCalories = "81 calories";
calories.setText(strCalories);
break;
case 5:
strCalories = "66 calories";
calories.setText(strCalories);
break;
case 6:
strCalories = "46 calories";
calories.setText(strCalories);
break;
case 7:
strCalories = "89 calories";
calories.setText(strCalories);
break;
}
}
private void initControls() {
// TODO Auto-generated method stub
// RadioGroup
rgMeal = (RadioGroup) findViewById (R.id.rgSelectMeal);
sp = (Spinner) findViewById (R.id.spFoodVegetable);
save = (Button) findViewById (R.id.btFoodVegetableSave);
calories = (Button) findViewById (R.id.btFoodVegetableCalories);
back = (Button) findViewById (R.id.tabs_back);
home = (Button) findViewById (R.id.tabs_home);
tv = (TextView) findViewById (R.id.txtMenuHeader);
etAmount = (EditText) findViewById (R.id.etAmount);
tv.setText(R.string.whitebread);
ArrayAdapter<String> array =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, classes);
sp.setAdapter(array);
sp.setOnItemSelectedListener(this);
back.setOnClickListener(this);
home.setOnClickListener(this);
save.setOnClickListener(this);
}
更新しました:
問題は、それが増殖していないということです。ボタンのテキストからカロリー値を取得し、それを編集テキストのユーザー入力で乗算します。