onEditTextChange无法获取整数数据

时间:2013-01-30 11:31:37

标签: android android-edittext

我有一个有edittext的应用程序。它已经有一个默认值(1)。然后,用户可以改变他想要的任何东西,例如他想要将其改为3.然后他可以从旋转器中选择食物值。每种食物都有相应的卡路里。

例如,我选择一个含有8卡路里的面包然后在edittext 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);

}                                                           

更新:

问题是,它不会成倍增加。我从按钮中的文本中获取卡路里值,然后将其与编辑文本中的用户输入相乘。

1 个答案:

答案 0 :(得分:0)

更好的方法是

 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[] calories = {188, 185, 126, 149, 81, 66, 46, 89};

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())
     int total = amount * calories[position];
     strCalories = total + " calories";
     calories.setText(strCalories);

}