Android onEditTextChange可以';无法获取整数数据

Android onEditTextChange可以';无法获取整数数据,android,android-edittext,Android,Android Edittext,我有一个有edittext的应用程序。它已经有一个默认值1。然后,用户可以将其更改为任何他想更改为3的内容。然后,他可以从微调器中选择食品价值。每种食物都有相应的卡路里 例如,我选择了一个含8卡路里的面包,然后在编辑文本2中输入。总热量应该是16卡路里。8 * 2. 请看我迄今为止所做的尝试: String[] classes = { "Cornbread", "French Bread", "French Toast", "French Toast,

我有一个有edittext的应用程序。它已经有一个默认值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);

}                                                           
String[]类={
“玉米面包”,
“法国面包”,
“法式吐司”,
“法式吐司,低脂”,
“意大利面包”,
“小麦面包”,
“小麦面包,低热量”,
小麦面包,全麦面包
};
字符串名称=已选择;
字符串服务=carries.getText().toString();
int i=Integer.parseInt(serving.replaceAll(“[\\D]”,“”));
String servng=String.valueOf(i);
int amount=Integer.valueOf(etAmount.getText().toString());
int answer=金额*i;
String strAnswer=String.valueOf(应答);
卡路里。塞特xt(strAnswer);
SimpleDataFormat sdf=新的SimpleDataFormat(“MM dd yyyy”);
字符串strDate=sdf.format(new Date());
if((mealname.isEmpty()| | servg.isEmpty()){
//要求定制吐司
viewErrorToast();
}已选择公共位置(AdapterView父对象、视图arg1、内部位置、,
长arg3){
//TODO自动生成的方法存根
selected=parent.getItemAtPosition(position.toString();
开关(位置){
案例0:
strCalories=“188卡路里”;
热量。setText(标准热量);
打破
案例1:
strCalories=“185卡路里”;
热量。setText(标准热量);
打破
案例2:
strCalories=“126卡路里”;
热量。setText(标准热量);
打破
案例3:
strCalories=“149卡路里”;
热量。setText(标准热量);
打破
案例4:
strCalories=“81卡路里”;
热量。setText(标准热量);
打破
案例5:
strCalories=“66卡路里”;
热量。setText(标准热量);
打破
案例6:
strCalories=“46卡路里”;
热量。setText(标准热量);
打破
案例7:
strCalories=“89卡路里”;
热量。setText(标准热量);
打破
}
}
私有控件(){
//TODO自动生成的方法存根
//放射组
rgdime=(放射组)findViewById(R.id.rgSelectDime);
sp=(微调器)findViewById(R.id.spFoodVegetable);
保存=(按钮)findViewById(R.id.btFoodVegetableSave);
卡路里=(按钮)findViewById(R.id.btFoodVegeTableCarries);
后退=(按钮)findViewById(R.id.tabs\u后退);
主页=(按钮)findViewById(R.id.tabs\u主页);
tv=(TextView)findViewById(R.id.txtMenuHeader);
etAmount=(EditText)findViewById(R.id.etAmount);
tv.setText(R.string.whitebread);
阵列自适应阵列=
新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,类);
sp.setAdapter(阵列);
sp.setOnItemSelectedListener(此);
back.setOnClickListener(this);
home.setOnClickListener(this);
save.setOnClickListener(这个);
}                                                           
更新:


问题是,它没有成倍增长。我从按钮中的文本中获取卡路里值,然后将其乘以编辑文本中的用户输入。

更好的方法是

 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);

}
String[]类={
“玉米面包”,
“法国面包”,
“法式吐司”,
“法式吐司,低脂”,
“意大利面包”,
“小麦面包”,
“小麦面包,低热量”,
小麦面包,全麦面包
};
//在这个数组中输入每种食物含有多少卡路里
int[]卡路里={188185126 14981 66 46 89};
已选择公共位置(AdapterView父对象、视图arg1、内部位置、,
长arg3){
//TODO自动生成的方法存根
selected=parent.getItemAtPosition(position.toString();
int amount=Integer.valueOf(etAmount.getText().toString())
int total=总量*卡路里[位置];
strCalories=总热量+卡路里;
热量。setText(标准热量);
}

您必须显示日志(以便我们猜测问题)或至少说明问题这里有什么问题?您提到的第二个编辑文本abt声明在哪里?我到处都看不到,你到底有什么问题?我已经更新了我的帖子。请检查一下。谢谢。你好,我试过这个。但每次我从edittext更改值时。这不是成倍增长。就像我输入2。它应该乘以卡路里。然后在edittext上放置一个textchange侦听器。在“文本更改后打开”中,更改卡路里值。或者有一个计算卡路里的点击按钮