我有一个应用程序要求用户输入他/她吃的量。我使用的输入字段是EditText。我想要发生的是当编辑文本发生变化时,它应该计算(即将金额乘以卡路里,如2面包量等于8卡路里。所以答案应该是16卡路里。)
但我无法让它发挥作用。请看看我尝试过的内容:
更新:
public class Bread_White extends Activity implements OnClickListener, OnItemSelectedListener {
Spinner sp;
Button calories, save, back, home;
String selected, strCalories;
TextView tv;
EditText etAmount;
int total;
RadioGroup rgMeal;
//RadioButton breakfast, ms, lunch, as, dinner, es;
String[] classes = {
"Cornbread",
"French Bread",
"French Toast",
"French Toast, low fat",
"Italian Bread",
"Wheat Bread",
"Wheat Bread, low calories",
"Wheat Bread, whole wheat"
};
//put how much calorie each food item has in this array
int[] intCalories = {188, 185, 126, 149, 81, 66, 46, 89};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.food_vegetable);
initControls();
}
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);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch( v.getId() ){
case R.id.btFoodVegetableSave:
String mealname = selected;
String serving = calories.getText().toString();
int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));
//int amount = Integer.valueOf(etAmount.getText().toString());
//int answer = amount * i;
String strAnswer = String.valueOf(i);
//calories.setText(strAnswer);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sdf.format(new Date());
if ( ( mealname.isEmpty() || strAnswer.isEmpty() ) ){
// call for custom toast
viewErrorToast();
}
else {
boolean didItWork = true;
try{
int checkedRadioButton = rgMeal.getCheckedRadioButtonId();
switch( checkedRadioButton ){
case R.id.rbBreakfast:
BreakFastLog bfLog = new BreakFastLog(Bread_White.this);
bfLog.open();
bfLog.createEntry(mealname, strAnswer, strDate);
bfLog.close();
break;
case R.id.rbMorningSnack:
MorningSnackLog msLog = new MorningSnackLog(Bread_White.this);
msLog.open();
msLog.createEntry(mealname, strAnswer, strDate);
msLog.close();
break;
case R.id.rbLunch:
LunchLog lunchLog = new LunchLog(Bread_White.this);
lunchLog.open();
lunchLog.createEntry(mealname, strAnswer, strDate);
lunchLog.close();
break;
case R.id.rbAfternoonSnack:
AfternoonSnackLog asLog = new AfternoonSnackLog(Bread_White.this);
asLog.open();
asLog.createEntry(mealname, strAnswer, strDate);
asLog.close();
break;
case R.id.rbDinner:
DinnerLog dinnerLog = new DinnerLog(Bread_White.this);
dinnerLog.open();
dinnerLog.createEntry(mealname, strAnswer, strDate);
dinnerLog.close();
break;
case R.id.rbEveningSnack:
EveningSnackLog esLog = new EveningSnackLog(Bread_White.this);
esLog.open();
esLog.createEntry(mealname, strAnswer, strDate);
esLog.close();
break;
}
}
catch(Exception e){
didItWork = false;
viewErrorToast();
}finally{
if (didItWork){
viewBMRSavedToast();
}
}
} // end of if else statement
break;
case R.id.tabs_back:
finish();
break;
case R.id.tabs_home:
Intent home = new Intent(this, IHealthFirstActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home);
break;
}
}
private void viewBMRSavedToast() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();
}
private void viewErrorToast() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
}
public void onItemSelected(AdapterView<?> parent, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
selected = parent.getItemAtPosition(position).toString();
int amount = Integer.valueOf(etAmount.getText().toString());
total = amount * intCalories[position];
calories.setText(strCalories);
TextWatcher watcher = new TextWatcher(){
public void afterTextChanged(Editable s) {
//your business logic after text is changed
strCalories = total + " calories";
calories.setText(strCalories);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
//your business logic before text is changed
}
public void onTextChanged(CharSequence s, int start, int before, int count){
//your business logic while text has changed
}
};
etAmount.addTextChangedListener(watcher);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
请帮忙解决这个问题。感谢。
答案 0 :(得分:0)
卡路里视图定义在哪里?你也许想做最后的决定。你能改一下这个问题吗?我无法找到这个问题:)
答案 1 :(得分:0)
将 selectedPos 声明为类级别变量
int selectedPos = 0;
public void afterTextChanged(Editable s) {
//your business logic after text is changed
int cal = intCalories[selectedPos];
total = (Integer.parseInt(s.toString)) * cal;
calories.setText(total + " calories");
}
在定义的变量中设置微调器的选定位置。
快乐编码:)