最近学习的Android 内容如下:
还有自己找的一些解释介绍:
ViewModel : https://www.jianshu.com/p/35d143e84d42
LiveData :https://www.jianshu.com/p/21bb6c0f8a5a
DataBinding : https://www.jianshu.com/p/d3d8f842b5ec
Vector Drawable : https://www.jianshu.com/p/c4c7906d5bc3
Screen Orientation Localization
按照视频所学制作了一个比赛计分器 https://www.bilibili.com/video/av50954019?p=13
效果图如下:
功能简介: 左右为队伍 A B的得分计数
点击进行分数添加 撤销按钮可返回一次上次的分数操作
重置按钮重置记录的成绩(点击home键之后可以读取保存数据并后台运行)
退出后 无法保存数据
需要添加一下代码
1 android.defaultConfig.vectorDrawables.useSupportLibrary = true //向下兼容 保证app在不同版本安卓系统的兼容性 2 dataBinding { //允许使用dataBinding 3 enabled true 4 }
MyViewModel 实现程序功能
1 package com.example.score; 2 3 import androidx.lifecycle.MutableLiveData; 4 import androidx.lifecycle.ViewModel; 5 6 public class MyviewModel extends ViewModel { 7 private static MutableLiveData<Integer> aTeamScore; 8 private static MutableLiveData<Integer> bTeamScore; 9 private static int aBack,bBack; 10 11 public MutableLiveData<Integer> getaTeamScore() { 12 if(aTeamScore==null){ //如果为空 则初始化 为 0 13 aTeamScore = new MutableLiveData<>(); 14 aTeamScore.setValue(0); 15 } 16 return aTeamScore; 17 } 18 19 public MutableLiveData<Integer> getbTeamScore() { 20 if(bTeamScore==null){ 21 bTeamScore = new MutableLiveData<>(); 22 bTeamScore.setValue(0); 23 } 24 return bTeamScore; 25 } 26 27 28 public void aTeamAdd(int p){ //对分数进行修改 29 aBack =aTeamScore.getValue(); //存储上一次操作的数据 30 bBack= bTeamScore.getValue(); 31 aTeamScore.setValue(aTeamScore.getValue()+p); 32 } 33 34 public void bTeamAdd(int p){ 35 aBack =aTeamScore.getValue(); //存储上一次操作的数据 存储两个避免两个分数同时被撤销 36 bBack= bTeamScore.getValue(); 37 bTeamScore.setValue(bTeamScore.getValue()+p); 38 } 39 40 public void reset(){ //重置操作 41 aBack =aTeamScore.getValue(); //存储上一次操作的数据 42 bBack= bTeamScore.getValue(); 43 aTeamScore.setValue(0); 44 bTeamScore.setValue(0); 45 } 46 47 public void undo(){ //撤销操作 48 aTeamScore.setValue(aBack); 49 bTeamScore.setValue(bBack); 50 } 51 }
MainActivity.java
在视频讲解当中使用了 ViewModelProviders
但是现版本已经去除 如果继续使用会是按钮点击但无法进行操作
改为 MyviewModel myviewModel = new ViewModelProvider(this).get(MyviewModel.class); 即可
1 package com.example.score; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import androidx.databinding.DataBindingUtil; 5 import androidx.lifecycle.ViewModelProvider; 6 //import androidx.lifecycle.ViewModelProviders; 7 8 import android.os.Bundle; 9 10 import com.example.score.databinding.ActivityMainBinding; 11 12 public class MainActivity extends AppCompatActivity { 13 MyviewModel myviewModel; 14 ActivityMainBinding binding; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 binding = DataBindingUtil.setContentView(this,R.layout.activity_main); 20 myviewModel = new ViewModelProvider(this).get(MyviewModel.class); 21 // ViewModelProviders.of(this).get(MyviewModel.class); 22 binding.setData(myviewModel); 23 binding.setLifecycleOwner(this); 24 } 25 }
databinding 修改后的 Activity_main.xml 的text
1 <?xml version="1.0" encoding="utf-8"?> 2 <layout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools"> 5 6 <data> //添加的变量 通过此修改下面的命令 7 <variable 8 name="data" 9 type="com.example.score.MyviewModel" /> 10 </data> 11 12 <androidx.constraintlayout.widget.ConstraintLayout 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 tools:context=".MainActivity"> 16 17 <androidx.constraintlayout.widget.Guideline 18 android:id="@+id/guideline" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:orientation="vertical" 22 app:layout_constraintGuide_percent="0.5" /> 23 24 <androidx.constraintlayout.widget.Guideline 25 android:id="@+id/guideline2" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:orientation="horizontal" 29 app:layout_constraintGuide_percent="0.05" /> 30 31 <androidx.constraintlayout.widget.Guideline 32 android:id="@+id/guideline3" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:orientation="horizontal" 36 app:layout_constraintGuide_percent="0.15" /> 37 38 <androidx.constraintlayout.widget.Guideline 39 android:id="@+id/guideline4" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:orientation="horizontal" 43 app:layout_constraintGuide_percent="0.35" /> 44 45 <androidx.constraintlayout.widget.Guideline 46 android:id="@+id/guideline5" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:orientation="horizontal" 50 app:layout_constraintGuide_percent="0.5" /> 51 52 <androidx.constraintlayout.widget.Guideline 53 android:id="@+id/guideline6" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:orientation="horizontal" 57 app:layout_constraintGuide_percent="0.65" /> 58 59 <androidx.constraintlayout.widget.Guideline 60 android:id="@+id/guideline7" 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:orientation="horizontal" 64 app:layout_constraintGuide_percent="0.8" /> 65 66 <androidx.constraintlayout.widget.Guideline 67 android:id="@+id/guideline8" 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:orientation="horizontal" 71 app:layout_constraintGuide_percent="0.9" /> 72 73 <TextView 74 android:id="@+id/textView" 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:text="@string/textview1" 78 android:textSize="@dimen/teamTextSize" 79 app:layout_constraintBottom_toTopOf="@+id/guideline3" 80 app:layout_constraintEnd_toStartOf="@+id/guideline" 81 app:layout_constraintStart_toStartOf="parent" 82 app:layout_constraintTop_toTopOf="@+id/guideline2" /> 83 84 <TextView 85 android:id="@+id/textView2" 86 android:layout_width="wrap_content" 87 android:layout_height="wrap_content" 88 android:text="@string/textview2" 89 android:textSize="@dimen/teamTextSize" 90 app:layout_constraintBottom_toTopOf="@+id/guideline3" 91 app:layout_constraintEnd_toEndOf="parent" 92 app:layout_constraintStart_toStartOf="@+id/guideline" 93 app:layout_constraintTop_toTopOf="@+id/guideline2" /> 94 95 <TextView 96 android:id="@+id/textView3" 97 android:layout_width="wrap_content" 98 android:layout_height="wrap_content" 99 android:text="@{String.valueOf(data.getaTeamScore())}" 100 android:textColor="@color/colorAccent" 101 android:textSize="@dimen/scoreTextSize" 102 app:layout_constraintBottom_toTopOf="@+id/guideline4" 103 app:layout_constraintEnd_toStartOf="@+id/guideline" 104 app:layout_constraintStart_toStartOf="parent" 105 app:layout_constraintTop_toTopOf="@+id/guideline3" 106 tools:text="120" /> 107 108 <TextView 109 android:id="@+id/textView4" 110 android:layout_width="wrap_content" 111 android:layout_height="wrap_content" 112 android:text="@{String.valueOf(data.getbTeamScore())}" 113 android:textColor="@color/colorPrimary" 114 android:textSize="@dimen/scoreTextSize" 115 app:layout_constraintBottom_toTopOf="@+id/guideline4" 116 app:layout_constraintEnd_toEndOf="parent" 117 app:layout_constraintStart_toStartOf="@+id/guideline" 118 app:layout_constraintTop_toTopOf="@+id/guideline3" 119 tools:text="88" /> 120 121 <Button 122 android:id="@+id/button" 123 android:layout_width="0dp" 124 android:layout_height="wrap_content" 125 android:background="@color/colorAccent" 126 android:text="@string/button1" 127 android:textColor="@android:color/white" 128 android:textSize="@dimen/buttonTextSize" 129 android:onClick="@{()->data.aTeamAdd(1)}" 130 app:layout_constraintBottom_toTopOf="@+id/guideline5" 131 app:layout_constraintEnd_toStartOf="@+id/guideline" 132 app:layout_constraintStart_toStartOf="parent" 133 app:layout_constraintTop_toTopOf="@+id/guideline4" /> 134 135 <Button 136 android:id="@+id/button2" 137 android:layout_width="0dp" 138 android:layout_height="wrap_content" 139 android:background="@color/colorPrimary" 140 android:text="@string/button1" 141 android:textColor="@android:color/white" 142 android:textSize="@dimen/buttonTextSize" 143 android:onClick="@{()->data.bTeamAdd(1)}" 144 app:layout_constraintBottom_toTopOf="@+id/guideline5" 145 app:layout_constraintEnd_toEndOf="parent" 146 app:layout_constraintStart_toStartOf="@+id/guideline" 147 app:layout_constraintTop_toTopOf="@+id/guideline4" /> 148 149 <Button 150 android:id="@+id/button3" 151 android:layout_width="0dp" 152 android:layout_height="wrap_content" 153 android:background="@color/colorAccent" 154 android:text="@string/button2" 155 android:textColor="@android:color/white" 156 android:textSize="@dimen/buttonTextSize" 157 android:onClick="@{()->data.aTeamAdd(2)}" 158 app:layout_constraintBottom_toTopOf="@+id/guideline6" 159 app:layout_constraintEnd_toStartOf="@+id/guideline" 160 app:layout_constraintStart_toStartOf="parent" 161 app:layout_constraintTop_toTopOf="@+id/guideline5" /> 162 163 <Button 164 android:id="@+id/button4" 165 android:layout_width="0dp" 166 android:layout_height="wrap_content" 167 android:background="@color/colorPrimary" 168 android:text="@string/button2" 169 android:textColor="@android:color/white" 170 android:textSize="@dimen/buttonTextSize" 171 android:onClick="@{()->data.bTeamAdd(2)}" 172 app:layout_constraintBottom_toTopOf="@+id/guideline6" 173 app:layout_constraintEnd_toEndOf="parent" 174 app:layout_constraintStart_toStartOf="@+id/guideline" 175 app:layout_constraintTop_toTopOf="@+id/guideline5" /> 176 177 <Button 178 android:id="@+id/button5" 179 android:layout_width="0dp" 180 android:layout_height="wrap_content" 181 android:background="@color/colorAccent" 182 android:text="@string/button3" 183 android:textColor="@android:color/white" 184 android:textSize="@dimen/buttonTextSize" 185 android:onClick="@{()->data.aTeamAdd(3)}" 186 app:layout_constraintBottom_toTopOf="@+id/guideline7" 187 app:layout_constraintEnd_toStartOf="@+id/guideline" 188 app:layout_constraintStart_toStartOf="parent" 189 app:layout_constraintTop_toTopOf="@+id/guideline6" /> 190 191 <Button 192 android:id="@+id/button6" 193 android:layout_width="0dp" 194 android:layout_height="wrap_content" 195 android:background="@color/colorPrimary" 196 android:text="@string/button3" 197 android:textColor="@android:color/white" 198 android:textSize="@dimen/buttonTextSize" 199 android:onClick="@{()->data.bTeamAdd(3)}" 200 app:layout_constraintBottom_toTopOf="@+id/guideline7" 201 app:layout_constraintEnd_toEndOf="parent" 202 app:layout_constraintStart_toStartOf="@+id/guideline" 203 app:layout_constraintTop_toTopOf="@+id/guideline6" /> 204 205 <ImageButton 206 android:id="@+id/imageButton" 207 android:layout_width="wrap_content" 208 android:layout_height="wrap_content" 209 android:contentDescription="@string/undobutton" 210 android:onClick="@{()->data.undo()}" 211 app:layout_constraintBottom_toTopOf="@+id/guideline8" 212 app:layout_constraintEnd_toStartOf="@+id/guideline" 213 app:layout_constraintHorizontal_bias="0.8" 214 app:layout_constraintStart_toStartOf="parent" 215 app:layout_constraintTop_toTopOf="@+id/guideline7" 216 app:srcCompat="@drawable/ic_undo_black_24dp" /> 217 218 <ImageButton 219 android:id="@+id/imageButton2" 220 android:layout_width="wrap_content" 221 android:layout_height="wrap_content" 222 android:contentDescription="@string/resetbutton" 223 android:onClick="@{()->data.reset()}" 224 app:layout_constraintBottom_toTopOf="@+id/guideline8" 225 app:layout_constraintEnd_toEndOf="parent" 226 app:layout_constraintHorizontal_bias="0.2" 227 app:layout_constraintStart_toStartOf="@+id/guideline" 228 app:layout_constraintTop_toTopOf="@+id/guideline7" 229 app:srcCompat="@drawable/ic_loop_black_24dp" /> 230 231 </androidx.constraintlayout.widget.ConstraintLayout> 232 </layout>
emmm 目前初学阶段只能照猫画虎的学。 努力下去,一定会有收获 。