android kotlin 子線程中調用界面UI組件崩潰


UI 只能在主線程內更新,子線程需要更新UI組件時可以這樣:

    fun fuck(){
        Executors.newSingleThreadExecutor().execute{
            // url request ...
            val visibility = View.VISIBLE

            Handler(mainLooper).post {
                val btn = findViewById<Button>(R.id.button)
                btn.visibility = visibility
            }
        }

        Thread{
            println("hi there")
            Handler(Looper.getMainLooper()).post{
                  val btn = findViewById<Button>(R.id.button)
            }
        }.start()
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM