You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio应用开发求助:实现三输入公式计算功能

Hey there! Let's get that calculation logic working for your bt_calculate button. I'll walk you through the exact steps to implement this, based on your formula: answer=1.25 × (Standby × Quiescent) + (1.75 × Alarm/2)

Step 1: Attach a click listener to your calculate button

First, you need to connect the button in your layout to your Java code and tell it what to do when clicked. In your Activity's onCreate() method (or wherever you're setting up your UI), add this code:

// Find the button by its ID (make sure this matches the ID in your XML layout)
Button calculateButton = findViewById(R.id.bt_calculate);

// Set up the click action
calculateButton.setOnClickListener(v -> {
    // All calculation logic will go inside this lambda
    performCalculation();
});

I've separated the calculation into a separate method performCalculation() to keep things clean—feel free to put the code directly inside the lambda if you prefer.

Step 2: Create the calculation method

Now let's write the performCalculation() method to handle retrieving inputs, doing the math, and showing the result:

private void performCalculation() {
    // 1. Get references to your input fields (replace these IDs with your actual UI element IDs)
    EditText standbyInput = findViewById(R.id.et_standby);
    EditText quiescentInput = findViewById(R.id.et_quiescent);
    EditText alarmInput = findViewById(R.id.et_alarm);
    TextView resultDisplay = findViewById(R.id.tv_result);

    // 2. Extract and convert input values from text to numbers
    double standbyValue, quiescentValue, alarmValue;
    try {
        // Trim whitespace to avoid errors from accidental spaces
        standbyValue = Double.parseDouble(standbyInput.getText().toString().trim());
        quiescentValue = Double.parseDouble(quiescentInput.getText().toString().trim());
        alarmValue = Double.parseDouble(alarmInput.getText().toString().trim());
    } catch (NumberFormatException e) {
        // Handle cases where user enters non-numeric text
        Toast.makeText(this, "Please enter valid numbers for all fields!", Toast.LENGTH_SHORT).show();
        return; // Exit the method if inputs are invalid
    }

    // 3. Execute your formula
    double answer = (1.25 * standbyValue * quiescentValue) + (1.75 * alarmValue / 2);

    // 4. Display the result (format to 2 decimal places for readability—adjust as needed)
    resultDisplay.setText(String.format("Calculated Result: %.2f", answer));
}
Key Notes to Keep in Mind
  • Match IDs: Double-check that the IDs used in findViewById() exactly match the android:id values in your XML layout (e.g., if your standby input is android:id="@+id/standby_edit", use R.id.standby_edit instead of et_standby).
  • Input Validation: The try-catch block ensures your app doesn't crash if someone enters text instead of numbers—you can customize the error message or add more checks (like preventing negative values if that makes sense for your use case).
  • Formatting: String.format("%.2f", answer) rounds the result to two decimal places. You can change this to %.1f for one decimal, or remove the format entirely if you want full precision.

If you run into specific issues (like errors in logcat, or the button not responding at all), feel free to share the relevant code snippets and I can help troubleshoot further!

内容的提问来源于stack exchange,提问作者Harni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:26:47