Ios Kotlin-按下按钮时运行打印语句
我是一名Ios Kotlin-按下按钮时运行打印语句,ios,swift,kotlin,printf,android-button,Ios,Swift,Kotlin,Printf,Android Button,我是一名Swiftdev,刚刚进入Kotlin,所以我不熟悉事情的运作方式 在Swift中,如果我创建一个按钮并向其添加一个操作/目标,然后在该操作中添加一个print语句它将在控制台中打印出来 lazy var myButton: UIButton = { let button = UIButton(type: .system) // create button button.addTarget(self, action: #selector(myButtonPress
Swift
dev,刚刚进入Kotlin
,所以我不熟悉事情的运作方式
在Swift
中,如果我创建一个按钮并向其添加一个操作/目标,然后在该操作中添加一个print语句
它将在控制台中打印出来
lazy var myButton: UIButton = {
let button = UIButton(type: .system)
// create button
button.addTarget(self, action: #selector(myButtonPressed), for: .touchUpInside)
return button
}()
@objc func myButtonPressed() {
print("this gets printed to the console")
}
但是在Kotlin
中,当我有一个print语句时
没有任何内容被打印到构建输出中
或事件日志中
val myButton: Button = findViewById(R.id.myButtonId)
myButton.setOnClickListener { myButtonPressed() }
private fun myButtonPressed() {
print("nothing gets printed to the console, I have to use the Toast function")
}
我必须使用
private fun myButtonPressed() {
Toast.makeText(this, "this briefly appears inside the emulator", Toast.LENGTH_SHORT).show()
}
我是做错了什么,还是应该这样做?我必须添加才能使用
Log.d()
,并且必须在调试模式下运行它:
import android.util.Log // *** 1. include this import statement ***
private val TAG = "MainActivity" // *** 2. add this constant, name it TAG and set the value to the name of the Activity ***
val myButton: Button = findViewById(R.id.myButtonId)
myButton.setOnClickListener { myButtonPressed() }
private fun myButtonPressed() {
Log.d(TAG, ">>>>> now this prints to the console <<<<<<") // *** 3. add the TAG inside the first param inside the Log.d statement ***
}
导入android.util.Log//***1。包括此导入声明***
private val TAG=“main活动”//***2。添加此常量,将其命名为标记,并将值设置为活动的名称***
val myButton:Button=findViewById(R.id.myButtonId)
myButton.setOnClickListener{myButtonPressed()}
私人娱乐myButtonPressed(){
Log.d(标记“>>>>>>>现在它将打印到控制台