Kotlin - запуск оператора печати при нажатии кнопки - PullRequest
1 голос
/ 09 ноября 2019

Я Swift dev и я только вхожу в Kotlin, поэтому я не знаю, как все это работает.

В Swift, если я создаю кнопку и добавляю действие/ target к нему, и в этом действии я добавляю print statement, который выводится на консоль.

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 statement, ничего не печаталось на Build Output ни Event Log

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")
}

enter image description here

Я должен использовать

private fun myButtonPressed() {
    Toast.makeText(this, "this briefly appears inside the emulator", Toast.LENGTH_SHORT).show()
}

Я делаю что-то не так илиЭто так, как он должен работать?

1 Ответ

1 голос
/ 09 ноября 2019

Мне пришлось добавить, чтобы использовать Log.d(), и мне пришлось запустить его в Debug mode:

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 ***
}

enter image description here

...