我让用户将他们的身高和体重输入EditText
,然后计算他们的BMI。我正在尝试将EditText
转换为int
,以便我可以进行计算。
EditText userHeight = FindViewById<EditText>(Resource.Id.editTextHeight);
EditText userWeight = FindViewById<EditText>(Resource.Id.editTextWeight);
int intuserweight = Convert.ToInt32(userWeight.Text);
intuserweight = int.Parse(userWeight.Text);
int intuserheight = Int32.Parse(userHeight.Text);
我已经尝试了两次转化,并且都说明了System.FormatException: Input string was not in a correct format
。
编辑Enter Intake Class的完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace AndroidDoctorApp
{
[Activity(Label = "Enter Intake")]
public class EnterIntake : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.EnterIntake);
EditText userDate = FindViewById<EditText>(Resource.Id.editTextDate);
EditText userTime = FindViewById<EditText>(Resource.Id.editTextTime);
EditText userHeight = FindViewById<EditText>(Resource.Id.editTextHeight);
EditText userWeight = FindViewById<EditText>(Resource.Id.editTextWeight);
Button btnBMI = FindViewById<Button>(Resource.Id.btnCalcBMI);
Button btnAddHistory = FindViewById<Button>(Resource.Id.buttonAddHistory);
Button btnViewHistory = FindViewById<Button>(Resource.Id.buttonViewHistory);
int bmi;
int intuserweight = int.Parse(userWeight.Text.ToString());
// intuserweight = int.Parse(userWeight.Text);
int intuserheight = Int32.Parse(userHeight.Text);
bmi = intuserweight / (intuserheight * intuserheight);
btnBMI.Click += (object sender, EventArgs e) =>
{
string toast = string.Format("Your BMI is: ", bmi);
Toast.MakeText(this, toast, ToastLength.Long).Show();
};
btnAddHistory.Click += (object sender, EventArgs e) =>
{
string toast = string.Format("BMI Added to History ");
Toast.MakeText(this, toast, ToastLength.Long).Show();
};
}
}
}
答案 0 :(得分:2)
使用此:
int userweight = int.Parse(userWeight.getText().toString());
编辑 int bmi; int intuserweight; int intuserheight;
btnBMI.Click += (object sender, EventArgs e) =>
{
intuserweight = int.Parse(userWeight.Text.ToString());
intuserheight = int.Parse(userHeight.Text.ToString());
bmi = intuserweight / (intuserheight * intuserheight);
string toast = string.Format("Your BMI is: ", bmi);
Toast.MakeText(this, toast, ToastLength.Long).Show();
};
试试这个。很抱歉任何语法错误(如果有的话),因为我是一个在C#中不多的Java人。并且不要忘记告诉它是否有效。
答案 1 :(得分:0)
你尝试类似
的东西int user_Weight=Integer.parseInt(userheight.gettext().toString());