代码之家  ›  专栏  ›  技术社区  ›  r_each

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

  •  0
  • r_each  · 技术社区  · 2 年前

    我在安卓系统(Xamarin)中进行的一个练习项目遇到了问题。这是一个年度收入侧项目,我只需要一个“计算”按钮。下面是连接xml代码中的数据点的方法代码,它有calculateButton。单击事件处理程序。

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

    因此,为了提供更多关于发生了什么的细节,我运行了Android模拟器,在4个编辑框中键入数字,点击计算(按钮),5个文本视图旁边不会弹出任何结果。有人看到这里有什么问题吗?我没有任何错误,所以现在我头疼。

    旁注,我原来是计算按钮的。单击+=CalculateButton_Click;此外,这是在MainActivity.cs lol中

    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
    }
    

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

    enter image description here

    1 回复  |  直到 2 年前
        1
  •  2
  •   Cybrosys    2 年前

    您没有调用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();
    }