英文:
Kotlin - Changing TextView content in popup layout from a different screen
问题
You can change the text of the TextView with the "popupItemDesc" id in the setOnClickListener() snippet by finding the TextView within the inflated view and then setting its text. Here's how you can do it in Kotlin:
hpPotImg.setOnClickListener(){
val window = PopupWindow(this)
val view = layoutInflater.inflate(R.layout.item_popup_tooltip, null)
window.contentView = view
val popupLayout = view.findViewById<ConstraintLayout>(R.id.popupHpPot)
val popupItemDescTextView = view.findViewById<TextView>(R.id.popupItemDesc)
// Change the text of the TextView
popupItemDescTextView.text = "Your new text goes here"
popupLayout.setOnClickListener(){window.dismiss()}
window.showAsDropDown(hpPotImg)
}
Replace "Your new text goes here" with the text you want to set for the TextView.
英文:
I have this simple layout with a textview, that pops up when being clicked on an image in a different layout.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/popupHpPot"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#000000">
<TextView
android:id="@+id/popupItemDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Potion that restores 3 health."
android:textColor="#FFEB3B"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code snippet handling the popup:
hpPotImg.setOnClickListener(){
val window = PopupWindow(this)
val view = layoutInflater.inflate(R.layout.item_popup_tooltip, null)
window.contentView = view
val popupLayout = view.findViewById<ConstraintLayout>(R.id.popupHpPot)
popupLayout.setOnClickListener(){window.dismiss()}
window.showAsDropDown(hpPotImg)
}
How could I change the text from the view with "popupItemDesc" id from the setOnClickListener() snippet?
答案1
得分: 0
你需要使用 findViewById 方法与父视图 popupLayout 结合使用,以获取 popupItemDesc 的子视图如下
val popupItemDesc = popupLayout.findViewById<TextView>(R.id.popupItemDesc)
所以代码会像这样
hpPotImg.setOnClickListener(){
val window = PopupWindow(this)
val view = layoutInflater.inflate(R.layout.item_popup_tooltip, null)
window.contentView = view
val popupLayout = view.findViewById<ConstraintLayout>(R.id.popupHpPot)
val popupItemDesc = popupLayout.findViewById<TextView>(R.id.popupItemDesc)
popupItemDesc.text = "hello text changed in parent"
popupLayout.setOnClickListener(){
// 你可以在这里自由使用它
popupItemDesc.text = "hello text changed in child popupItemDesc inside popup"
window.dismiss()
}
window.showAsDropDown(hpPotImg)
}
英文:
You will need to use findViewById method with the parent view popupLayout to get the child view of popupItemDesc as below
val popupItemDesc = popupLayout.findViewById<TextView>(R.id.popupItemDesc)
so the code will be like this
hpPotImg.setOnClickListener(){
val window = PopupWindow(this)
val view = layoutInflater.inflate(R.layout.item_popup_tooltip, null)
window.contentView = view
val popupLayout = view.findViewById<ConstraintLayout>(R.id.popupHpPot)
val popupItemDesc = popupLayout.findViewById<TextView>(R.id.popupItemDesc)
popupItemDesc.text = "hello text changed in parent"
popupLayout.setOnClickListener(){
// you can use it here freely also
popupItemDesc.text = "hello text changed in child popupItemDesc inside popup"
window.dismiss()
}
window.showAsDropDown(hpPotImg)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论