今天学习了ViewModel,其是Jetpack的一个类,它可以将界面中的数据独立出来,这样不会造成页面上信息的丢失。

我跟着视频做了一个简单的实例:

首先创建项目的时候它和以往的项目会有些不一样,因为需要使用Jetpack库,所以需要勾选上Use legacy android.support libraries。

我们需要再com....这个文件夹下新建一个Jjava class.来表示实物类。

我的例子就是实现按+1按钮或着+2按钮,再Textview 上显示i相应的数值。

MainActivity:

package com.example.viewmodeltext;

import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;
TextView textView;
Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);//获取对象 textView = findViewById(R.id.textView);
textView.setText(String.valueOf(myViewModel.number));
button1 = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewModel.number+=1;
textView.setText(String.valueOf(myViewModel.number));
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myViewModel.number+=2;
textView.setText(String.valueOf(myViewModel.number));
}
});
}
}

新建的类(MyViewModel):

package com.example.viewmodeltext;

import android.arch.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
public int number = 0;
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.475"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.355" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.562" />
</android.support.constraint.ConstraintLayout>

android开发基础(ViewModel)的更多相关文章

  1. 20145213 《Java程序设计》实验四 Android开发基础

    20145213 <Java程序设计>实验四 Android开发基础 说在前面的话 不同以往实验,对于这次实验具体内容我是比较茫然的.因为点我,打开实验四的链接居然能飘出一股熟悉的味道,这 ...

  2. 20145206实验四《Android开发基础》

    20145206 实验四<Android开发基础> 实验内容 ·安装Android Studio ·运行安卓AVD模拟器 ·使用安卓运行出虚拟手机并显示HelloWorld以及自己的学号 ...

  3. 实验四 Android开发基础

    实验四 Android开发基础 实验内容 1.安装Android Studio 2.运行安卓AVD模拟器 3.使用安卓运行出虚拟手机并显示HelloWorld以及自己的学号 (一)SDK的安装 (二) ...

  4. 20145337实验四Android开发基础

    20145337实验四Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件处 ...

  5. 20145225《Java程序设计》 实验四 Android开发基础

    20145225<Java程序设计> 实验四 Android开发基础 实验报告 实验内容 安装Android Studio 运行安卓AVD模拟器 使用安卓运行出虚拟手机并显示HelloWo ...

  6. 20145208 实验四 Android开发基础

    20145208 实验四 Android开发基础 安装Android Studio 安装的具体步骤在老师的链接中已经很详细了,在此就不做赘述了. 在此提出我觉得安装的时候需要注意的两个地方 一是安装地 ...

  7. 20145215实验四 Android开发基础

    20145215实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...

  8. 20165223 实验四 Android开发基础

    实验四 Android开发基础 目录 一.实验报告封面 二.具体实验内容 (一)Android Stuidio的安装测试 (二)Activity测试 (三)UI测试 (四)布局测试 (五)教材代码测试 ...

  9. 20155324 《Java程序设计》实验四 Android开发基础

    20155324 <Java程序设计>实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件. ...

  10. 2017-2018-2 20165237 实验四《Android开发基础》实验报告

    2017-2018-2 20165237 实验四<Android开发基础>实验报告 实验报告表头: No.1 实验要求: Android程序设计-1 实验要求: 参考<Java和An ...

随机推荐

  1. AcWing 891. Nim游戏

    //a1 ^ a2 ^ ··· ^ an = 0 –>先手必败: //a1 ^ a2 ^ ··· ^ an != 0 –>先手必胜: #include<iostream> us ...

  2. php自带的密码哈希

    常用的MD5.SHA1.SHA256哈希算法,是面向快速.高效进行哈希处理而设计的.随着技术进步和计算机硬件的提升,如今强大的计算机很容易破解这种算法.也就是说,不要用MD5.SHA1.SHA256这 ...

  3. python,装饰器带参数,原理

    import time # 函数装饰器传参 def zhuang1(hao): def zhuang2(func1): def funczhuang(*args, **kw): print(time. ...

  4. Mysql5.6基础命令

    Centos7下mysql5.6数据库的操作 Mysql如何修改密码? 1.使用mysqladmin修改,这种修改方式需要知道mysql的原始密码 修改密码后我们测试下看看能不能登录成功 怎么才能不需 ...

  5. javasE--基础部分--线程

    Day23 本单元目标 一.线程的相关概念  二.线程的创建和启动★ 三.线程的停止 四.线程的常用方法 五.线程的生命周期★  六.线程的同步 七.线程的通信 八.线程的创建方式三 九.线程的创建方 ...

  6. 简单了解一个WSDL,SOAP的格式

    简单了解一个WSDL,SOAP的格式 2016-06-20 09:34:43 树先生i 阅读数 5187  收藏 更多 分类专栏: WebService   版权声明:本文为博主原创文章,遵循CC 4 ...

  7. 在Linux系统上安装Jenkins

    1.首先准备安装java环境,安装jdk 详情查看博客以,这里不做多介绍. 2.下载Jenkins至Linux服务器 查看内核版本信息:cat /proc/version uname -m cat / ...

  8. AcWing 895. 最长上升子序列

    //设上升序列的最后一个数字为第i个,那么就以第i-1个位分类标准, //i-1可以没有,也可以是在数组中下标为1,下标为2 //一直到下标为i-1的数字 #include <iostream& ...

  9. pikaqiu练习平台(XSS(跨站脚本))

    XSS(跨站脚本)概述 Cross-Site Scripting 简称为“CSS”,为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS.一般XSS可以分为如下几种常见类型: ...

  10. Game of Credit Cards

    After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle betwe ...