android mvvm基础的使用

添加绑定

android {
	...
    dataBinding {
        enabled = true
    }
}

编写布局

  • 要以layout作为根布局
  • 使用data将布局和数据模型进行绑定
  • 定义变量的名称和变量类型
  • 给对应的控件使用对应变量的属性和方法
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="viewModel"
            type="com.chery.mvvmdouban.viewmodel.LoginViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@mipmap/login_back"
            android:scaleType="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/logo" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="34dp"
                android:background="@mipmap/user_edit"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="16sp">

                <EditText
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:hint="请输入用户名"
                    android:text="@{viewModel.account}"
                    android:textColor="@color/textColor"
                    android:textColorHint="@color/textColorHint"
                    android:textSize="16sp"
                     />


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@mipmap/user_edit"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="16sp">



                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:text="@{viewModel.password}"
                    android:textColor="@color/textColor"
                    android:textColorHint="@color/textColorHint"
                    android:textSize="16sp" />

            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="8dp"
                    android:text="忘记密码"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="16sp" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@mipmap/btn_login"
                android:orientation="vertical"
                android:padding="6dp">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?selectableItemBackground"
                    android:text="登录"
                    android:textColor="@color/viewLineColor"
                    android:textSize="18sp"
                    android:onClick="@{viewModel.onClickView}"
                 />
            </LinearLayout>

            <TextView
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="模拟一个登陆操作,随便输入账号密码点击登录即可进入"
                android:textColor="#EE1010" />
        </LinearLayout>
    </RelativeLayout>
</layout>

定义实体类

  • 实体类通过Obserable属性值的改变会立即触发 UI 刷新
package com.chery.mvvmdouban.viewmodel;
import android.app.Activity;
import android.databinding.ObservableField;
import android.view.View;
import com.dejun.commonsdk.wight.ToastUtil;
/**
 * @author DcotorWei
 * @date :2019/11/26 14:46
 * @description :TODO
 * @email :1348172474@qq.com
 */
public class LoginViewModel {
    public ObservableField<String> account=new ObservableField<>();
    public ObservableField<String> password=new ObservableField<>();

    public LoginViewModel(String account,String password) {
       this.account.set(account);
       this.password.set(password);
    }
    //登录按钮监听
    public void onClickView(View view){
        ToastUtil.MakeToast(((Activity)view.getContext()),"点击事件",ToastUtil.LENGTH_LONG).show();
    }
}

使用

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActivityLoginBinding activityLoginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        activityLoginBinding.setViewModel(new LoginViewModel("DoctorWei","123456"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值