2016-05-12 34 views
0

我是C#和Xamarin的新手,我可能會錯誤地介紹這個解決方案,但是我聲明的值將通過用戶輸入0-10數字獲得,而不是小數點後面沒有負數。這將做一個基本的數學運算a/b * c = answer ...然後我想顯示var C(答案)並最終使用它來改變一個計時器間隔。然而現在,我很難讓代碼顯示我的答案作爲文本供用戶查看....請參閱下面的代碼。Xamarin中的C#計算,顯示答案的問題。

[Activity(Label = "Infusion Calculator")] 
public class infusionact : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     SetContentView(Resource.Layout.Infusion); 
     // Create your application here 
     var volume = FindViewById<EditText>(Resource.Id.boxvolume); 
     var drip = FindViewById<EditText>(Resource.Id.boxdrip); 
     var dripmins = FindViewById<EditText>(Resource.Id.boxmins); 
     var answermins = (Resource.Id.boxvolume/Resource.Id.boxmins * Resource.Id.boxdrip); 

     Button button = FindViewById<Button>(Resource.Id.btncalculate); 
     TextView textView1 = (TextView)FindViewById(Resource.Id.textView1); 

     button.Click += delegate 
     { 
      // NEED TO FIGURE OUT HOW TO SET TXT LABEL WITH VAR ANSWERMINS ON CLICK 
      textView1.SetText(answermins); 

     }; 



    } 

} 

回答

1

我認爲你濫用這些變量。例如,

var volume = FindViewById<EditText>(Resource.Id.boxvolume); 

返回關聯到指定ID的VIEW,反之,

var volumeValue = volume.Text; 

將返回value已輸入的輸入您的EditText控制。它的這些值需要處理,然後顯示在您的TextView上。

0

刪除該行是因爲您使用資源ID來執行計算,而不是EditText中的值。

var answermins = (Resource.Id.boxvolume/Resource.Id.boxmins * Resource.Id.boxdrip); 

更新click事件來完成計算。

button.Click += delegate 
    { 
     var volumeValue = 0; 
     var dripValue = 0; 
     var dripMinsValue = 0; 

     // Parse value in text to integer 
     int.TryParse(volume.Text, out volumeValue); 
     int.TryParse(drip.Text, out dripValue); 
     int.TryParse(dripmins.Text, out dripMinsValue); 

     var answermins = 0; 
     if (dripMinsValue != 0) 
     { 
      answermins = volumeValue/dripMinsValue * dripValue; 
     } 

     textView1.SetText(answermins); 
    }; 
0

這是正確的代碼 -

[Activity(Label = "Infusion Calculator")] 
public class infusionact : Activity 
{ 
protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 

    SetContentView(Resource.Layout.Infusion); 
    // Create your application here 
    var volume = FindViewById<EditText>(Resource.Id.boxvolume); 
    var drip = FindViewById<EditText>(Resource.Id.boxdrip); 
    var dripmins = FindViewById<EditText>(Resource.Id.boxmins); 

    Button button = FindViewById<Button>(Resource.Id.btncalculate); 
    TextView textView1 = FindViewById<TextView>(Resource.Id.textView1); 

    button.Click += delegate 
    { 
     // NEED TO FIGURE OUT HOW TO SET TXT LABEL WITH VAR ANSWERMINS ON CLICK 
     var answermins = volume.Text/(dripmins.Text*drip.Text); 
      textView1.Text=answermins.ToString(); 

    }; 



} 
}