为什么即使启动了事件处理程序并创建了button_click方法,单击按钮后也没有结果?

问题描述 投票:0回答:1

我在 Android(Xamarin) 中制作的实践副项目遇到问题。这是一个年收入副项目,我只有一个“计算”按钮。下面是在 xml 代码中连接数据点的方法代码,它具有calculateButton.Click 事件处理程序。

下一段代码是 click 方法本身,它应该返回用户输入的项目。我还将包含 EditText 和 TextView 变量。

因此,为了提供有关所发生情况的更多详细信息,我运行 Android 模拟器,在 4 个编辑框中输入数字,单击计算(按钮),并且 5 个 Textview 旁边没有弹出任何结果。有人看出这里有什么问题吗?我没有任何错误,所以现在我头疼。

附注,我原本有calculateButton.Click +=CalculateButton_Click;另外,这是在 MainActivity.cs 中,哈哈

EditText incomePerHourEditText;
EditText workHoursEditText;
EditText taxRateEditText;
EditText savingsRateEditText;

TextView workSummaryTitle;
TextView annualIncomeTitle;
TextView annualTaxTitle;
TextView annualSavingsTitle;
TextView spendableIncomeTitle;

Button calculateButton;
RelativeLayout resultLayout;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);
}

void ConnectViews()
{
    incomePerHourEditText = FindViewById<EditText>(Resource.Id.incomePerHourEditText);
    workHoursEditText = FindViewById<EditText>(Resource.Id.workHoursEditText);
    taxRateEditText = FindViewById<EditText>(Resource.Id.taxRateEditText);
    savingsRateEditText = FindViewById<EditText>(Resource.Id.savingsRateEditText);

    workSummaryTitle = FindViewById<TextView>(Resource.Id.workSummaryTitle);
    annualIncomeTitle = FindViewById<TextView>(Resource.Id.annualIncomeTitle);
    annualTaxTitle = FindViewById<TextView>(Resource.Id.annualTaxTitle);
    annualSavingsTitle = FindViewById<TextView>(Resource.Id.annualSavingsTitle);
    spendableIncomeTitle = FindViewById<TextView>(Resource.Id.spendableIncomeTitle);

    calculateButton = FindViewById<Button>(Resource.Id.calculateButton);
    resultLayout = FindViewById<RelativeLayout>(Resource.Id.resultLayout);

    calculateButton.Click += new EventHandler(CalculateButton_Click);

}
protected void CalculateButton_Click(object sender, System.EventArgs e)
{
    //Take inputs from user
    double incomePerHour = double.Parse(incomePerHourEditText.Text);
    double workHourPerDay = double.Parse(workHoursEditText.Text);
    double taxRate = double.Parse(taxRateEditText.Text);
    double savingsRate = double.Parse(savingsRateEditText.Text);

    double annualWorkHoursSummary = workHourPerDay * 5 * 50; //50 because most get 2 weeks off
    double annualIncome = incomePerHour * workHourPerDay * 5 * 50;
    double taxPayable = (taxRate / 100) * annualIncome;
    double annualSavings = (savingsRate / 100) * annualIncome;
    double spendableIncome = annualIncome - annualSavings - taxPayable;

    //Display results of the calculations
    workSummaryTitle.Text = annualWorkHoursSummary.ToString()+" Hrs";
    annualIncomeTitle.Text = annualIncome.ToString()+ " USD";
    annualTaxTitle.Text = taxPayable.ToString() + " USD";
    annualSavingsTitle.Text = annualSavings.ToString() + " USD";
    spendableIncomeTitle.Text = spendableIncome.ToString() + " USD";

    //display the Resultlayout from invisible to visible
}

这就是我期待的结果。但是,这些数字不会显示在年度 ____ 旁边的右下角。帮助;-;

c# xml xamarin xamarin.android
1个回答
1
投票

您没有调用 ConnectViews 方法。改变这个:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);
}

对此:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);

    ConnectViews();
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.