Android Studio - 学习笔记(JetPack) - Day 2 - 霜冷的秘密基地

Android Studio - 学习笔记(JetPack) - Day 2

程序设计 0 评

前言:本文记录学习安卓开发笔记,用于快速查找功能 - [Day 2]

  • 存储器 - 创建名为MyData的存储器并存储Int:100,data->data->MyData
SharedPreferences shp = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
//存储Int 100, key为NUMBER
editor.putInt("NUMBER", 100);
editor.apply();

//读取Int并打印 , key:NUMBER
int x = shp.getInt("NUMBER", 0);
String TAG = "xNumber";
Log.d(TAG, "onCreate: " + x);
  • SharedPreferences - MyData.class 存储和读取功能实现类
import android.content.Context;
import android.content.SharedPreferences;
public class MyData {
    private Context context;
    //传入context[getApplicationContext()]实现在这个类里面访问全局资源
    //例如context.getResources().getString(R.string.MY_DATA)
    public MyData(Context context){
        this.context = context;
    }

    public void save(int number){
        String name = context.getResources().getString(R.string.MY_DATA);
        String key = context.getResources().getString(R.string.MY_KEY);
        SharedPreferences shp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
        editor.putInt(key, number);
        editor.apply();
    }

    public int load(){
        String name = context.getResources().getString(R.string.MY_DATA);
        String key = context.getResources().getString(R.string.MY_KEY);
        Integer key_def = context.getResources().getInteger(R.integer.KEY_DEF);
        SharedPreferences shp = context.getSharedPreferences(name, Context.MODE_PRIVATE);

        int x = shp.getInt(key, key_def);
        return x;
    }
}
//MainActivity中使用
MyData myData = new MyData(getApplicationContext());
myData.save(1000);
int number = myData.load();
String TAG = "myNumber";
Log.d(TAG, "onCreate: " + number);
  • ViewModel + LiveData 全局资源访问,保存在本地 - MyViewModel类的实现与应用
public class MyViewModel extends AndroidViewModel {
    SavedStateHandle handle;
    String key = getApplication().getString(R.string.data_key);
    String shpName = getApplication().getString(R.string.shp_name);

    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        //空值时加载数据
        if(!handle.contains(key)){
            load();
        }
    }
    //获取当前UI数据
    public LiveData<Integer> getNumber(){
        return handle.getLiveData(key); // UI当前数据
    }
    //加载xml中保存的数据
    private void load(){
        SharedPreferences shp = getApplication().getSharedPreferences(shpName, Context.MODE_PRIVATE);
        handle.set(key, shp.getInt(key, 0));    //空值时设定数据,缺省为0
    }
    //保存当前UI数据
    public void save(){
        SharedPreferences shp = getApplication().getSharedPreferences(shpName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
        editor.putInt(key, getNumber().getValue());
        editor.apply();
    }
    //当前UI数据增加x值
    public void add(int x){
        handle.set(key, getNumber().getValue() + x);
    }
}
//=================
//MainActivity.java
public class MainActivity extends AppCompatActivity {
    MyViewModel myViewModel;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        myViewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(MyViewModel.class);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        myViewModel.save(); //进入后台写入数据
    }
}
//=================
//xml
<data>
    <variable
        name="data"
        type="com.example.viewmodelshp.MyViewModel" />
</data>
//textView data
android:text="@{String.valueOf(data.getNumber())}"

//button onclick
android:onClick="@{()->data.add(1)}"
Python爬虫 - 图集谷图片爬取
快来做第一个评论的人吧~
0:00