Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Kotlin-按下按钮时运行打印语句_Ios_Swift_Kotlin_Printf_Android Button - Fatal编程技术网

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(标记“>>>>>>>现在它将打印到控制台