| Android中MutableLiveData的使用:
 1.观察者模式的简单运用:Android工程实例
 <font color#999AAA>01.创建MutableLiveData实例,观察并响应其变化 class MainActivity : AppCompatActivity() {
    private var mutableLiveData = MutableLiveData<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        observe()
        findViewById<Button>(R.id.btn_change).setOnClickListener {
            
            mutableLiveData.value = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(System.currentTimeMillis()))
        }
    }
    private fun observe() {
        
        mutableLiveData.observe(this, { _t ->
            val str = _t ?: ""
            
            runOnUiThread {
                
                findViewById<EditText>(R.id.show_text).setText(str)
            }
        })
    }
}
 
 |