【问题标题】:Converting EditText into Int将 EditText 转换为 Int
【发布时间】:2016-05-30 15:31:52
【问题描述】:

我让用户在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

编辑进入课程的完整代码:

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

        };


    }
}

}

【问题讨论】:

  • 您可能首先要检查userWeight.Text 的样子

标签: c# android string android-edittext int


【解决方案1】:

使用这个:

int userweight = int.Parse(userWeight.getText().toString());

编辑 国际体重指数; 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();
    };

试试这个。对于任何语法错误(如果有的话),我深表歉意,因为我是一个 Java 人,在 C# 中并不多。并且不要忘记告诉它是否有效。

【讨论】:

  • 是的,我试过了,但 getText 没有出现 'Android.Widget.EditText' 不包含 'getText' 的定义,并且没有扩展方法 'getText' 接受类型为'Android'的第一个参数.Widget.EditText' 可以找到(您是否缺少 using 指令或程序集引用?)
  • 然后使用 .Text.toString()
  • 我试过了,它出现了同样的错误 System.FormatException:输入字符串的格式不正确。 int userweight = int.Parse(userWeight.Text.ToString());
  • 尝试打印重量值。检查返回的值是什么?
  • 因为它是 Visual Studio 中的一个 editText 框,允许用户在 android 中输入。我打印了输入,但什么也没显示。
【解决方案2】:

你可以试试

int user_Weight=Integer.parseInt(userheight.gettext().toString());

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2013-02-08
    • 2020-10-10
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多