美文网首页
data绑定 kotlin

data绑定 kotlin

作者: alex_zn | 来源:发表于2018-07-20 17:24 被阅读0次
    <?xml version="1.0" encoding="utf-8"?>
    <layout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
    
        <data>
            <import type="com.example.zzn.myapplication.Main2Activity.Action"/>
            <import type="com.example.zzn.myapplication.Person"/>
            <variable
                name="action"
                type="Action"/>
            <variable
                name="model"
                type="Person"/>
        </data>
        
        <android.support.constraint.ConstraintLayout
    
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.example.zzn.myapplication.Main2Activity">
    
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{model.name}"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="16dp"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginEnd="8dp"/>
            <Button
                android:text="按钮"
                android:onClick="@{action.tap}"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_marginTop="32dp"
                app:layout_constraintTop_toBottomOf="@+id/textView"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintHorizontal_bias="0.502"/>
        </android.support.constraint.ConstraintLayout>
        
    </layout>
    
    public class Main2Activity : AppCompatActivity() {
    
        private var receiver:MyReceiver? = null
        private val mNotiName = "com.example.zzn.myapplication.MY_NOTI"
        
        //绑定事件
         inner class Action {
            
            //点击事件
            fun tap(view:View) {
                
                Toast.makeText(view.context,"发送广播",Toast.LENGTH_SHORT).show()
                val intent = Intent(this@Main2Activity.mNotiName)
                sendBroadcast(intent)
                
            }
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    //        setContentView(R.layout.activity_main2)
            
            val binding = DataBindingUtil.setContentView<ActivityMain2Binding>(this,R.layout.activity_main2)
            val person =Person()
            person.name = "zhanshan"
            person.age = "11"
            
            binding.action = Action()
            binding.model = person
            setBoardCast()
            
        }
        
        //动态广播
        fun setBoardCast() {
            
            receiver = MyReceiver()
            val filter = IntentFilter(mNotiName)
            registerReceiver(receiver,filter)
            
        }
    }
    
    //
    public class Person {
        
        var name:String? = null
        var age:String? = null
    }
    public class MyReceiver : BroadcastReceiver() {
    
        override fun onReceive(context: Context, intent: Intent) {
    
            Toast.makeText(context, "收到广播", Toast.LENGTH_LONG).show()
        }
    }

    相关文章

      网友评论

          本文标题:data绑定 kotlin

          本文链接:https://www.haomeiwen.com/subject/tjpqmftx.html