前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kotlin与Databinding(一)简介封装

Kotlin与Databinding(一)简介封装

作者头像
code_horse
发布2018-07-02 11:11:37
7120
发布2018-07-02 11:11:37
举报
文章被收录于专栏:Android NoteAndroid NoteAndroid Note

简介

使用ObservableField<T>来初始化变量,例如:var userPhone=ObservableField<String>()

封装

这时候我们需要对model类进行一层封装才能达到我们的要求,好了,上图再解释。

1.png

解释

因为使用databinding是双向绑定,所以咱们这里就比较方便的来直接判断变量中的值即可。因为这里是我自己写的Demo,所以逻辑判断是比较简单的,大家如果使用按照自己的需求网上添加即可。

xml代码

<data class="RegisterBind">

    <variable
        name="activity"
        type="com.kotlin.databinding.zhihu.activity.user.RegisterActivity"/>

    <variable
        name="registerModel"
        type="com.kotlin.databinding.zhihu.model.RegisterModel"/>
</data>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingLeft="0dp"
        android:paddingRight="@dimen/margin_block">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginEnd="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginStart="5dp"
            android:contentDescription="手机号"
            android:scaleType="centerInside"
            android:src="@drawable/account_phone"/>

        <View
            android:layout_width="1dp"
            android:layout_height="18dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="34dp"
            android:layout_marginStart="34dp"
            android:background="@color/divider"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="48dp"
            android:layout_marginStart="48dp"
            android:hint="请输入您的手机号码">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/user_mobile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:gravity="center_vertical"
                android:imeOptions="actionNext"
                android:inputType="phone"
                android:maxLength="11"
                android:singleLine="true"
                android:text="@={registerModel.userPhone}"
                android:textColor="@color/font_title"
                android:textSize="@dimen/font_normal"/>
        </android.support.design.widget.TextInputLayout>

        <com.kotlin.databinding.zhihu.widget.CountButton
            android:id="@+id/countButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:minHeight="24dp"
            android:background="@null"
            android:layout_gravity="end|center_vertical"
            android:gravity="center_vertical"
            android:text="@{@string/timer_start}"
            android:textColor="@color/color_primary_dark"
            android:textSize="12sp"
            bind:setDefaultTime="@{@integer/integer_count_time}"
            bind:setFinishText="@{@string/timer_finish}"/>


    </FrameLayout>

    <View
        android:layout_width="34dp"
        android:layout_height="1dp"
        android:background="@android:color/white"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:paddingLeft="0dp"
        android:paddingRight="@dimen/margin_block">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginEnd="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginStart="5dp"
            android:contentDescription="密码"
            android:scaleType="centerInside"
            android:src="@drawable/account_password"/>

        <View
            android:layout_width="1dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="34dp"
            android:layout_marginStart="34dp"
            android:background="@color/divider"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="48dp"
            android:layout_marginStart="48dp"
            android:gravity="center_vertical"
            android:hint="请输入您的密码">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/user_password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:gravity="center_vertical"
                android:inputType="textPassword"
                android:singleLine="true"
                android:text="@={registerModel.userPwd}"
                android:textColor="@color/font_title"
                android:minLines="6"
                android:maxLength="20"
                android:textSize="@dimen/font_normal"/>
        </android.support.design.widget.TextInputLayout>

        <CheckBox
            android:id="@+id/check_visible"
            style="@style/PasswordCheckboxTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|center_vertical"
            android:gravity="center"
            android:minHeight="24dp"/>

    </FrameLayout>

    <View
        android:layout_width="34dp"
        android:layout_height="1dp"
        android:background="@android:color/white"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:paddingLeft="0dp"
        android:paddingRight="@dimen/margin_block">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginEnd="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginStart="5dp"
            android:contentDescription="验证码"
            android:src="@drawable/ic_identify"/>

        <View
            android:layout_width="1dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="34dp"
            android:layout_marginStart="34dp"
            android:background="@color/divider"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="48dp"
            android:layout_marginStart="48dp"
            android:gravity="center_vertical"
            android:hint="请输入您的验证码">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/phone_identify_code"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:gravity="center_vertical"
                android:inputType="number"
                android:singleLine="true"
                android:text="@={registerModel.idenfityCode}"
                android:textColor="@color/font_title"
                android:textSize="@dimen/font_normal"/>
        </android.support.design.widget.TextInputLayout>

        <com.kotlin.databinding.zhihu.widget.CheckView
            android:id="@+id/change_identify_code"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="24dp"
            android:layout_gravity="end|center_vertical"
            checkview:bg_color="@color/color_primary_light"
            checkview:line_num="10"
            checkview:point_num="100"
            checkview:text_color="#FF00FFFF"
            checkview:text_length="4"
            checkview:text_size="30dp"/>

    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/cut_line_color" />

    <Button
        android:id="@+id/register_btn"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_margin="15dp"
        android:background="@drawable/bg_btn_main"
        android:enabled="false"
        android:text="注册"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_title"
        bind:onClickListener="@{activity}"
        />
</LinearLayout>

xml的布局是这样的,好了,咱们看看怎么赋值吧。

具体调用

registBind.activity = this //给button注册监听
registBind.registerModel = registModel //给xml赋值model类
registModel.btnIsEnable(registModel) //监听EditText的值是否合法

//初始化model类,用回调的值更新button的isEnabled属性
var registModel = RegisterModel { isBtnEnable -> isBtnEnable(isBtnEnable) }
//更新button状态
fun isBtnEnable(isBtnEnable: Boolean) {
    registBind.registerBtn.isEnabled = isBtnEnable
 }

好了,到这里databinding监听EditText的用法就讲完了。有什么疑问可以留言。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.07.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 封装
    • 解释
      • xml代码
        • 具体调用
    相关产品与服务
    验证码
    腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档