android开发基础(ViewModel)
<p>今天学习了ViewModel,其是Jetpack的一个类,它可以将界面中的数据独立出来,这样不会造成页面上信息的丢失。</p><p>我跟着视频做了一个简单的实例:</p>
<p>首先创建项目的时候它和以往的项目会有些不一样,因为需要使用Jetpack库,所以需要勾选上Use legacy android.support libraries。</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1718499/202002/1718499-20200209191045823-319432986.png" alt=""></p>
<p> </p>
<p> </p>
<p> 我们需要再com....这个文件夹下新建一个Jjava class.来表示实物类。</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1718499/202002/1718499-20200209191242803-208787193.png" alt=""></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/i-beta/1718499/202002/1718499-20200209191329741-1645965256.png" alt=""></p>
<p> </p>
<p> </p>
<p> 我的例子就是实现按+1按钮或着+2按钮,再Textview 上显示i相应的数值。</p>
<p><img src="https://img2018.cnblogs.com/i-beta/1718499/202002/1718499-20200209191648419-1152883486.png" alt=""></p>
<p> </p>
<p> </p>
<p> MainActivity:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.viewmodeltext;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.arch.lifecycle.ViewModel;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.arch.lifecycle.ViewModelProviders;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.support.v7.app.AppCompatActivity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Button;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.TextView;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> MainActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AppCompatActivity {
MyViewModel myViewModel;
TextView textView;
Button button1,button2;
@Override
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
</span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewModel </span>= ViewModelProviders.of(<span style="color: rgba(0, 0, 255, 1)">this</span>).get(MyViewModel.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);//获取对象
textView </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.textView);
textView.setText(String.valueOf(myViewModel.number));
button1 </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.button);
button2 </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.button2);
button1.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
myViewModel.number</span>+=1<span style="color: rgba(0, 0, 0, 1)">;
textView.setText(String.valueOf(myViewModel.number));
}
});
button2.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
myViewModel.number</span>+=2<span style="color: rgba(0, 0, 0, 1)">;
textView.setText(String.valueOf(myViewModel.number));
}
});
}
}</span></pre>
</div>
<p>新建的类(MyViewModel):</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.viewmodeltext;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.arch.lifecycle.ViewModel;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> MyViewModel <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> ViewModel {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> number = 0<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>activity_main.xml:</p>
<div class="cnblogs_code">
<pre><?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"<span style="color: rgba(0, 0, 0, 1)">
xmlns:app</span>="http://schemas.android.com/apk/res-auto"<span style="color: rgba(0, 0, 0, 1)">
xmlns:tools</span>="http://schemas.android.com/tools"<span style="color: rgba(0, 0, 0, 1)">
android:layout_width</span>="match_parent"<span style="color: rgba(0, 0, 0, 1)">
android:layout_height</span>="match_parent"<span style="color: rgba(0, 0, 0, 1)">
tools:context</span>=".MainActivity">
<<span style="color: rgba(0, 0, 0, 1)">TextView
android:id</span>="@+id/textView"<span style="color: rgba(0, 0, 0, 1)">
android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:text</span>="@string/textview"<span style="color: rgba(0, 0, 0, 1)">
android:textSize</span>="30sp"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintBottom_toBottomOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintEnd_toEndOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintHorizontal_bias</span>="0.475"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintStart_toStartOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintTop_toTopOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintVertical_bias</span>="0.144" />
<<span style="color: rgba(0, 0, 0, 1)">Button
android:id</span>="@+id/button"<span style="color: rgba(0, 0, 0, 1)">
android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:text</span>="@string/button1"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintBottom_toBottomOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintEnd_toEndOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintHorizontal_bias</span>="0.498"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintStart_toStartOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintTop_toTopOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintVertical_bias</span>="0.355" />
<<span style="color: rgba(0, 0, 0, 1)">Button
android:id</span>="@+id/button2"<span style="color: rgba(0, 0, 0, 1)">
android:layout_width</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:layout_height</span>="wrap_content"<span style="color: rgba(0, 0, 0, 1)">
android:text</span>="@string/button2"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintBottom_toBottomOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintEnd_toEndOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintHorizontal_bias</span>="0.498"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintStart_toStartOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintTop_toTopOf</span>="parent"<span style="color: rgba(0, 0, 0, 1)">
app:layout_constraintVertical_bias</span>="0.562" />
</android.support.constraint.ConstraintLayout></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/yangxionghao/p/12288342.html
頁:
[1]