android Databindign的使用

Databindign的使用

    • 定义实体类
    • 布局中设置
    • 代码中使用

android Databindign的使用_第1张图片

定义实体类

package com.weiqun.cherytelematic.bean;

/**
 * @author DcotorWei
 * @date :2019/11/25 8:52
 * @description :TODO
 * @email :[email protected]
 */
public class UserBean {
    /**
     * version : 版本号
     * businessId :
     * serviceType :
     * globalId : 登录时可以传空字符串
     * requestId : 请求id
     * login : {"userAccount":"登录账号","userPasswd":"登录密码"}
     */

    private String version;
    private String businessId;
    private String serviceType;
    private String globalId;
    private String requestId;
    private LoginBean login;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public String getServiceType() {
        return serviceType;
    }

    public void setServiceType(String serviceType) {
        this.serviceType = serviceType;
    }

    public String getGlobalId() {
        return globalId;
    }

    public void setGlobalId(String globalId) {
        this.globalId = globalId;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public LoginBean getLogin() {
        return login;
    }

    public void setLogin(LoginBean login) {
        this.login = login;
    }

    public static class LoginBean {
        /**
         * userAccount : 登录账号
         * userPasswd : 登录密码
         */

        private String userAccount;
        private String userPasswd;

        public String getUserAccount() {
            return userAccount;
        }

        public void setUserAccount(String userAccount) {
            this.userAccount = userAccount;
        }

        public String getUserPasswd() {
            return userPasswd;
        }

        public void setUserPasswd(String userPasswd) {
            this.userPasswd = userPasswd;
        }
    }
}

布局中设置

  1. 添加layout根布局
  2. 添加variable标签
  3. 在相应的控件中使用
<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="LoginBean"
            type="com.weiqun.cherytelematic.bean.UserBean.LoginBean" />

        <variable
            name="OnClickListener"
            type="android.view.View.OnClickListener" />
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rl_login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    

        <EditText
            android:id="@+id/et_loginUser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"
            android:background="@null"
            android:gravity="center|left"
            android:hint="用户名"
            android:paddingLeft="12dp"
            android:text="@{LoginBean.userAccount}"
            android:textColor="#6b6b6b"
            android:textSize="18sp" />


        <EditText
            android:id="@+id/et_loginPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="8dp"
            android:background="@null"
            android:gravity="center|left"
            android:hint="密码"
            android:paddingLeft="12dp"
            android:password="true"
            android:text="@{LoginBean.userPasswd}"
            android:textColor="#6b6b6b"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="49dp"
            android:layout_marginLeft="49dp"
            android:layout_marginRight="49dp"
            android:background="@mipmap/button_login"
            android:onClick="@{OnClickListener}" />
    </LinearLayout>
</layout>

代码中使用

  1. 获取对应类的Databinding
  2. 设置监听和数据
package com.weiqun.cherytelematic.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.dejun.commonsdk.base.BaseActivity;
import com.gyf.barlibrary.ImmersionBar;
import com.weiqun.cherytelematic.R;
import com.weiqun.cherytelematic.bean.UserBean;
import com.weiqun.cherytelematic.databinding.ActivityLoginBinding;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    ActivityLoginBinding activityLoginBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).init();
        activityLoginBinding=DataBindingUtil.setContentView(this,R.layout.activity_login);
        activityLoginBinding.setOnClickListener(this);
        //修改登录界面
        activityLoginBinding.setLoginBean(new UserBean.LoginBean("DoctorWei","123456"));
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        if (id==R.id.btn_login){
            openActivty(MainActivity.class,true);
        }
    }
}

你可能感兴趣的:(android Databindign的使用)