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()
}