Xamarin中的C#计算,显示答案的问题。

时间:2016-05-12 13:31:50

标签: c# android xamarin xamarin.android xamarin-studio

我是C#和Xamarin的新手,我可能会错误地解决这个问题,但是我宣布将通过用户输入获得的值0-10数字没有小数没有负数。这将进行基本的数学运算a / b * c = answer ...然后我想显示var 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);

        };



    }

}

3 个答案:

答案 0 :(得分:1)

我认为你在滥用这些变量。例如,

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

返回与指定ID相关联的VIEW,而

var volumeValue = volume.Text;

会返回您输入的value作为EditText控件的输入。您需要处理这些值,然后在TextView上显示。

答案 1 :(得分:0)

删除该行是因为您使用资源ID进行计算而不是EditText中的值。

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

更新点击事件以进行计算。

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

答案 2 :(得分: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();

    };



}
}