Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

android - How to make a function when pressing a button in KOTLIN

I created a function called "talk" so I want to press a button to execute "talk". I put this to do in the activity_main.xml and everything works fine. The problem comes when I don't want to do it by activity_main but by code in Mainactivity.kt. After the "talk" function is executed I want to make a condition. and I don't know how to do both when pressing the button.

imageButton.setOnClickListener{
talk()
if (textView3.text=="hello") {
textView.visibility = View.VISIBLE
} else {

}
}

this is the code of the function ""talk"" :

fun talk(v: View) {
        val intentActionRecognizeSpeech = Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
        // Configura el Lenguaje (Espa?ol-México)
        intentActionRecognizeSpeech.putExtra(
                RecognizerIntent.EXTRA_LANGUAGE_MODEL, "es-MX")
        try {
            startActivityForResult(intentActionRecognizeSpeech,
                    RECOGNIZE_SPEECH_ACTIVITY)
        } catch (a: ActivityNotFoundException) {
            Toast.makeText(applicationContext,
                    "Tú dispositivo no soporta el reconocimiento por voz",
                    Toast.LENGTH_SHORT).show()
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

At the time of initialization give visibility of textview then onclick give visibility to visible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...