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

用Kotlin实现按钮触发EditText值相加显示至TextView时遇显示问题

Hey there! Let's troubleshoot why your addition result isn't showing up in the TextView. This is a super common issue, so let's go through the most likely fixes step by step:

1. Double-Check Your Button Click Binding

First, make sure your button's click event is properly hooked up—this is where a lot of folks slip up.

Option 1: XML android:onClick method

If you're using XML to bind the click, ensure the method name matches exactly (case-sensitive!) with a public method in your Activity that accepts a View parameter:

<!-- In your layout XML -->
<Button
    android:id="@+id/add_button"
    android:text="Calculate Sum"
    android:onClick="computeSum" />
// In your Activity class
public void computeSum(View view) {
    // Your calculation logic goes here
}

Option 2: Programmatic click listener

If you're setting the listener in code, confirm you've correctly referenced the button ID after calling setContentView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button addBtn = findViewById(R.id.add_button);
    addBtn.setOnClickListener(v -> {
        // Run your calculation here
    });
}
2. Fix EditText Input Handling

Most of the time, the issue is with how you're reading and converting the EditText values. Here's the safe way to do it:

EditText num1Et = findViewById(R.id.num1_edittext);
EditText num2Et = findViewById(R.id.num2_edittext);
TextView resultTv = findViewById(R.id.result_textview);

// Get trimmed text to avoid whitespace issues
String num1Str = num1Et.getText().toString().trim();
String num2Str = num2Et.getText().toString().trim();

// Handle empty inputs first
if (num1Str.isEmpty() || num2Str.isEmpty()) {
    resultTv.setText("Please enter both numbers!");
    return;
}

// Convert to numbers with error handling (critical!)
try {
    int num1 = Integer.parseInt(num1Str);
    int num2 = Integer.parseInt(num2Str);
    int sum = num1 + num2;
    // Update the TextView with the result
    resultTv.setText(String.valueOf(sum));
} catch (NumberFormatException e) {
    // Catch non-numeric input and inform the user
    resultTv.setText("Enter valid numbers only!");
    e.printStackTrace(); // For debugging in logcat
}

The try-catch block is key here—if a user enters non-numeric text, your app won't crash, and you'll know exactly why the result isn't showing.

3. Verify TextView Basics
  • Double-check that the TextView ID in your code matches exactly what's in your XML layout.
  • Ensure the TextView isn't set to android:visibility="gone" or android:visibility="invisible"—you won't see the result even if you set it correctly!
  • If you're using View Binding/Data Binding, confirm you've initialized the binding object properly in onCreate.
4. Debug with Logs

If you're still stuck, add log statements to see where things are breaking:

Log.d("SumDebug", "Num1 input: " + num1Str);
Log.d("SumDebug", "Num2 input: " + num2Str);
Log.d("SumDebug", "Calculated sum: " + sum);

Check Logcat for these messages—if the sum shows up correctly here but not in the TextView, the issue is with the TextView itself (ID, visibility, or uninitialized reference).

If none of these fixes work, share your full layout XML and Activity code snippets, and we can dig deeper!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:00:27