2017-03-20 1 views
-2

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

     } 

    } 
} 

답변

1

당신은 버튼을 클릭 후 합 을 수행해야합니다.

는 또한 여기처럼 이동

:

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

      Ergebnis1.Text = ergebnis.ToString(); 
     }; 

} 

그러나, 당신은 당신이 사용자 입력을 형성 읽은 값을 사용하고 있지 않습니다. 웬일인지, 당신은 99를 사용한다. 당신은 그것을 바꿀 수있다.

+0

안녕하세요! 고맙습니다! 아직 시도했지만, 결과로 0이되었습니다 ... 그래서 나는 무언가를 시험해보기 위해 99로 int를 설정했습니다. – user7285912

0

당신은 얻을 필요 :이 코드와 함께 작동하도록 그것을 가지고 아래와 같이 클릭 내 EditText 값 :

//output result if button is clicked 
buttonErgebnis.Click += (sender, e) => 
{ 
    //Need to get values of Zahl1 and Zahl2 here 
    string str1 = Zahl1 .Text; 
    string str2 = Zahl1 .Text; 
    // add two numbers 
    int ergebnis = str1 + str2 ; 
    Ergebnis1.Text = ergebnis.ToString(); 
}; 
+0

감사합니다. 하지만 여전히 나에게 0을 제공합니다. ( – user7285912