android-fragments 如何在文本视图中更改文本值,Kotlin在片段中,

1szpjjfi  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(162)

有没有人知道一种方法来改变一个文本视图的文本值,而在一个片段。我已经尝试findviewbyid,但我相信这是行不通的,而在一个片段。有没有人知道如何识别一个文本视图,并改变它的文本值。谢谢

class Home : Fragment() {

    override fun onCreateView(

        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view =  inflater.inflate(R.layout.fragment_home, container, false)

        val queue = Volley.newRequestQueue(view.context)
        val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" //URL
        var urlStuff = ""

        val stringRequest = StringRequest(Request.Method.GET, url,
            { response ->
                // Display the first 500 characters of the response string.
                urlStuff = response
                val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
                val id = jsonArray.getJSONObject(0).getString("value") // Get data
                Log.i("Val: ", id)

                //Updating info on app
                val textView = view.findViewById(R.id.temp)                 
                textView.text = "My Text"

            },
            { Log.i("b", "That didn't work!") })
        queue.add(stringRequest)
        return view

    }

}
eanckbw9

eanckbw91#

findViewById也可以在片段中工作,如下所示:

val view =  inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = view.findViewById(R.id.temp)
textView.text = "My Text"

相关问题