首页 > 其他分享 >2022-10-28学习内容

2022-10-28学习内容

时间:2022-10-28 21:25:21浏览次数:72  
标签:10 name weight age married 28 height 2022 et

1.SharedPreferences用法

1.1activity_share_write.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入姓名"
            android:inputType="text"
            android:maxLength="12"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入年龄"
            android:inputType="number"
            android:maxLength="2"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="身高:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入身高"
            android:inputType="number"
            android:maxLength="3"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_weight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="体重:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入体重"
            android:inputType="numberDecimal"
            android:maxLength="5"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:gravity="center"
        android:text="已婚"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存到共享参数"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

1.2ShareWriteActivity.java

package com.example.chapter06;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_write);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);

        findViewById(R.id.btn_save).setOnClickListener(this);

        preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        reload();
    }

    private void reload() {
        String name = preferences.getString("name", null);
        if (name != null) {
            et_name.setText(name);
        }
        int age = preferences.getInt("age", 0);
        if (age != 0) {
            et_age.setText(String.valueOf(age));
        }
        float height = preferences.getFloat("height", 0f);
        if (height != 0f) {
            et_height.setText(String.valueOf(height));
        }
        float weight = preferences.getFloat("weight", 0f);
        if (height != 0f) {
            et_weight.setText(String.valueOf(weight));
        }
        boolean married = preferences.getBoolean("married", false);
        ck_married.setChecked(married);
    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name", name);
        editor.putInt("age", Integer.parseInt(age));
        editor.putFloat("height", Float.parseFloat(height));
        editor.putFloat("weight", Float.parseFloat(weight));
        editor.putBoolean("married", ck_married.isChecked());
        editor.commit();

    }
}

1.3效果:

 退出应用,重新进入,从SharedPreferences中读取:

 

 SharedPreferences文件位置:

 

 

 

 1.4config.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="name">ceshi</string>
    <float name="weight" value="110.0" />
    <boolean name="married" value="true" />
    <int name="age" value="27" />
    <float name="height" value="170.0" />
</map>

 

标签:10,name,weight,age,married,28,height,2022,et
From: https://www.cnblogs.com/pingfanliliang/p/16834548.html

相关文章

  • windows10我的电脑打不开
    我们在使用win10专业版系统时总会遇到各种情况或问题,最近就有用户反馈说自己的win10系统出现了我的电脑打不开的状况,那我们该如何解决呢?下面小编就为大家分享了具体的解决......
  • 10道Python面试题
    1、Python里面如何拷贝一个对象?(赋值,浅拷贝,深拷贝的区别)答:赋值(=),就是创建了对象的一个新的引用,修改其中任意一个变量都会影响到另一个。浅拷贝:创建一个新的对象,但它包......
  • 【CFgym102870J】Junction of Orz Pandas(思维,容斥)
    暴力做法就不会做……考虑容斥,用所有数\(\leqa_i\)的方案减去所有数\(<a_i\)的方案得到最大值为\(a_i\)的方案,\(b_i\)同理容斥,时间复杂度\(O(2^{n+m}nm)\)。直......
  • 【CFgym102482D】Gem Island(生成函数)
    题意:有一个序列\(a_1,\cdots,a_n\),初始时它们全为\(1\)。进行\(d\)轮操作:每轮操作以正比于\(a\)的概率选择一个\(a_i\)加\(1\)。求最后\(a_1,\cdots,a_n\)中前......
  • 中文美句_Oct.28
    "我爱在黎明前起床,在山顶牧场上召唤太阳,云雀的歌声便是我异想天开的翅膀,朝露便是我晨起的浴缸。   ......
  • CSP-S2022 游记
    10.27进行了一个试的考,但是一道都不会,\(40pts\)垫底。被xy的满分爆搜打爆了。感觉周二和周四的考试应该swap一下。然后讲题发现前3题都好简单。T1就是爆搜,我太蠢......
  • 【10.22-10.28】博客精彩回顾
    一、优秀文章推荐1.​​MySQL上亿大表如何优化?​​​2.​​使用Shell脚本进行语句循环​​3.​​数据结构【C语言版】二叉树的结构和遍历的实现​​4.​​构建FTP文件传输......
  • CSP-2022游记
    Day-2早上打了场正睿模拟赛,炸了110分,总分170(60+70+0+40)(T3输出格式错误少了70分,T1特殊情况挂分+subtask)。不知道正式考试会不会炸成这样。黄队轻松350(100+100+70+80),要被......
  • 2022-2023-1 20221421 《计算机基础与程序设计》第九周学习总结
    作业信息班级链接:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK09作业目标:cpu调度,进程控制,先到先......
  • (2022最新版)记录一下微信步数支付宝步数一件拉满教程
    本工具支持微信、支付宝、QQ,按照下面的图文教程,一步一步来,非常简单,几分钟搞定~第一步:下载zepp1、在手机的应用商店搜索下载App:zepp(这边已zepp为例子,下载Zepp......