C#으로 첫 번째 앱을 테스트하고 있습니다. axml 파일에는 4 개의 필드가 있습니다. 두 개의 숫자를 입력 할 수있는 2 개의 필드, 1 개의 버튼 및 버튼을 클릭 할 때 사용자가 입력 한 두 개의 숫자를 더한 결과를 출력하는 하나의 필드가 있습니다. 응용 프로그램이 작동하지만 내 버튼을 클릭하면 항상 출력이 0이됩니다. 그래서 문제는 내가 변수 EditText
에서 읽은 것일 수 있습니다. 도구 상자의 다른 필드를 사용해야합니까? Visual Studio로 작성 중입니다! 여기 내 코드는 다음과 같습니다.EditText 값은 0입니까?
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
EditText zahl1 = FindViewById<EditText>(Resource.Id.zahl1);
EditText zahl2 = FindViewById<EditText>(Resource.Id.zahl2);
Button buttonErgebnis = FindViewById<Button>(Resource.Id.buttonErgebnis);
TextView Ergebnis1 = FindViewById<TextView>(Resource.Id.emptyText);
//convert string to int and save in variable
string a = zahl1.ToString();
int Zahl1 = 99;
int.TryParse(a, out Zahl1);
string b = zahl2.ToString();
int Zahl2 = 99;
int.TryParse(a, out Zahl2);
// add two numbers
int ergebnis = Zahl1 + Zahl2;
//output result if button is clicked
buttonErgebnis.Click += (sender, e) =>
{
Ergebnis1.Text = ergebnis.ToString();
};
}
문제점을 확인할 수 있습니까? 나는 최대
머리 ... 내 프로그램이 사용자 권한에서 처음 두 숫자를 읽고 있지 함께 할 수있는 뭔가가 생각 :
using Android.App;
using Android.Widget;
using Android.OS;
namespace OverWriteGPS
[Activity(Label = "OverWriteGPS", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
EditText zahl1 = FindViewById<EditText>(Resource.Id.zahl1);
EditText zahl2 = FindViewById<EditText>(Resource.Id.zahl2);
Button buttonErgebnis = FindViewById<Button>(Resource.Id.buttonErgebnis);
TextView Ergebnis1 = FindViewById<TextView>(Resource.Id.emptyText);
//output result if button is clicked
buttonErgebnis.Click += (sender, e) =>
{
//convert string to int and save in variable
string a = zahl1.Text;
string b = zahl2.Text;
int Zahl1, Zahl2, ergebnis;
if ((int.TryParse(a, out Zahl1)) && (int.TryParse(b, out Zahl2)))
{
ergebnis = Zahl1 + Zahl2;
} else
{
ergebnis = -1;
}
Ergebnis1.Text = ergebnis.ToString();
};
}
}
}
안녕하세요! 고맙습니다! 아직 시도했지만, 결과로 0이되었습니다 ... 그래서 나는 무언가를 시험해보기 위해 99로 int를 설정했습니다. – user7285912