【发布时间】:2020-12-01 10:30:30
【问题描述】:
我的目标是使用 VS2019 (Xamarin) 创建一个 Android 应用程序。这个应用程序有 2 个输入字段(类型“numberDecimal”)。用户可以输入 2 个数字,应用程序无需按按钮或其他方式将其分开。结果应该显示在 TextView 中。
示例:第一个数字 = 2,54 |||第二个数字 = 13,44 |||结果 = 5,29
到目前为止,我已经为第一个数字创建了“EW”,为第二个数字创建了“EV”。我认为两者都需要转换为双格式(“EW2”,“EV2”),所以“结果”可以将它们分开。
但我总是收到错误消息“System.FormatException:** '输入字符串格式不正确。” 也许我做错了什么,或者可能是一个简单的。但是经过几天的搜索和尝试不同的方式,我陷入了死胡同……
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace App2
{
[Activity(Label = "VD-Rechner", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView VD_Ergebnis;
EditText EW;
EditText EV;
double EW2;
double EV2;
double result;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
VD_Ergebnis = FindViewById<TextView>(Resource.Id.Ergebnis);
EW = FindViewById<EditText>(Resource.Id.Einwaage);
EV = FindViewById<EditText>(Resource.Id.Endvolumen);
EW2 = double.Parse(EW.Text); //ERROR-MESSAGE HERE
EV2 = double.Parse(EV.Text);
result = EV2 / EW2;
VD_Ergebnis.Text = result.ToString();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="59.0dp"
android:text="Verdünnungsfaktor"
android:textSize="25sp"
android:id="@+id/Ergebnis"
android:textColor="#ff00c853"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:inputType="numberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:id="@+id/Einwaage"
android:textColor="#ffffd600"
android:typeface="serif" />
<TextView
android:text="Einwaage"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/EinwaageText"
android:textColor="#ffffd600"
android:typeface="serif" />
<EditText
android:inputType="numberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:id="@+id/Endvolumen"
android:textColor="#ff2979ff"
android:typeface="serif" />
<TextView
android:text="Endvolumen"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/EndvolumenText"
android:textColor="#ff2979ff"
android:typeface="serif" />
</LinearLayout>
```
【问题讨论】: