Kotlin – 从不同屏幕更改弹出布局中的TextView内容

huangapple go评论97阅读模式
英文:

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.

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/popupHpPot&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;70dp&quot;
    android:background=&quot;#000000&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/popupItemDesc&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Potion that restores 3 health.&quot;
        android:textColor=&quot;#FFEB3B&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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&lt;ConstraintLayout&gt;(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&lt;TextView&gt;(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&lt;ConstraintLayout&gt;(R.id.popupHpPot)
        val popupItemDesc = popupLayout.findViewById&lt;TextView&gt;(R.id.popupItemDesc)

        popupItemDesc.text = &quot;hello text changed in parent&quot;

        popupLayout.setOnClickListener(){
            // you can use it here freely also
            popupItemDesc.text = &quot;hello text changed in child popupItemDesc inside popup&quot;

            window.dismiss()
        }
        window.showAsDropDown(hpPotImg)
    }

huangapple
  • 本文由 发表于 2023年4月19日 22:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定