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)
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.
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)); }
- Match IDs: Double-check that the IDs used in
findViewById()exactly match theandroid:idvalues in your XML layout (e.g., if your standby input isandroid:id="@+id/standby_edit", useR.id.standby_editinstead ofet_standby). - Input Validation: The
try-catchblock 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%.1ffor 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

