kotlin 单击按钮时setOnClickList不起作用

2wnc66cl  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(132)

因此,我试图在Android Studio中使用Kotlin构建一个小型计算器应用程序。当用户按下添加按钮时,两个数字都应该计算并显示在textView中,但当我按下按钮时什么也没有发生。请帮助我解决这个问题。
我得到未解决的引用错误,请看一下
MainActivity.kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    addButton.setOnClickListener {
        addition()
    }
}
fun addition() {
    val num1Text = numberFirst.toString()
    val num2Text = numberSecond.toString()
    // Check if the input is empty
    if (num1Text.isEmpty() || num2Text.isEmpty()) {
        resultView = "Please enter valid numbers".toInt()
        return
    }
    // Convert the input to integers and perform addition
    val num1 = num1Text.toInt()
    val num2 = num2Text.toInt()
    val sum = num1 + num2
    resultView = "Result: $sum".toInt() // Display the result in 
    the TextView
   }
  }

字符串
这是活动主文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/textView4"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:text="Simple Calculator App"
    android:textAlignment="center"
    android:textSize="30dp" />

<EditText
    android:ems="10"
    android:hint="Enter first number"
    android:id="@+id/numberFirst"
    android:inputType="number"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="300dp" />

<EditText
    android:ems="5"
    android:hint="Enter second number"
    android:id="@+id/numberSecond"
    android:inputType="number"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="300dp" />

<TextView
    android:id="@+id/resultView"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"

    android:textSize="25dp" />

<LinearLayout
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/addButton"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:layout_width="120dp"
        android:text="+"
        android:textSize="25dp" />
</LinearLayout>

wh6knrhe

wh6knrhe1#

试试这个>

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val addButton = findViewById<Button>(R.id.addButton) // Button element with ID addButton

    addButton.setOnClickListener {
        addition()
    }
}

fun addition() {
    val num1Text = findViewById<EditText>(R.id.numberFirst).text.toString() // EditText with ID numberFirst
    val num2Text = findViewById<EditText>(R.id.numberSecond).text.toString() // EditText with ID numberSecond
    val resultView = findViewById<TextView>(R.id.resultView) // TextView with ID resultView

    if (num1Text.isEmpty() || num2Text.isEmpty()) {
        resultView.text = "Please enter valid numbers"
        return
    }

    val num1 = num1Text.toInt()
    val num2 = num2Text.toInt()
    val sum = num1 + num2

    resultView.text = "Result: $sum"
   }
 }

字符串

相关问题