'In Kotlin, how can I choose which textview a button should show the text?
I have two textviews in Kotlin, say Textview No 1 and Textview No. 2. I want buttons to show their text in one of the textviews that I select. E.g. Button "A" to show the text A in the first textview when I click the textview No. 1, and Button "B" to show the text B in the textview No. 2. It is similar to a calculator, just that calculators have only one textview. I am interested to show only the text, not mathematical calculations.
Thank you
Solution 1:[1]
You should be able to do something like this:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val textViewA: TextView? = view.findViewById(R.id.textViewA)
val textViewB: TextView? = view.findViewById(R.id.textViewB)
val buttonA: Button? = view.findViewById(R.id.buttonA)
val buttonB: Button? = view.findViewById(R.id.buttonB)
buttonA?.setOnClickListener {
textViewA?.text = buttonA.text
}
buttonB?.setOnClickListener {
textViewB?.text = buttonB.text
}
}
Solution 2:[2]
Think of it this way.
What are the events you are handling? Event 1: Click of button 1 Event 2: Click of button 2
on click of Event 1 what do you want to do -> choose which textview to show the text -> use an AlertDialog to select between the two textviews and depending on that show the text.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
| Solution 2 |
