Adroid Studio 实验六

这篇实验报告详细介绍了如何在Android Studio中使用SharedPreferences进行数据存储,比较了不同文件存储方式,并展示了SQLite数据库的创建和操作过程。通过四个实验题目,涵盖了SharedPreferences的使用,内部存储文件读写,SD卡文件操作以及资源文件访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《Android移动信息系统开发》实验报告

实验名称:第六次实验
实验日期:2022.06.20

一、实验目的
1.掌握SharedPreferences的使用方法
2.掌握各种文件存储的区别与适用情况
3.了解SQLite数据库的特点和体系结构
4.掌握SQLite数据库的建立和操作方法

二、实验具体内容
1、实验题目1:
(1)题目
SimplePreferenceDemo 说明SharedPreferences的文件保存位置和保存格式
(2)分析

(3)实验代码及运行结果
此处打印源代码(.xml文件和.java文件等),并将运行结果截图。
1.xml文件如下:
activity_main…xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/name"
        android:text=""
        android:layout_width="280dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"/>

    <TextView
        android:id="@+id/name_label"
        android:text="姓名:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toRightOf="@+id/name"
        android:layout_alignBaseline="@+id/name"/>

    <EditText
        android:id="@+id/age"
        android:text=""
        android:layout_width="280dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:layout_below="@id/name"
        android:numeric="integer"/>

    <TextView
        android:id="@+id/age_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toRightOf="@id/age"
        android:layout_alignBaseline="@id/age"
        android:text="年龄:"/>

    <EditText
        android:id="@+id/height"
        android:layout_width="280dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:layout_below="@id/age"
        android:numeric="decimal"/>

    <TextView
        android:id="@+id/height_label"
        android:text="身高"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toRightOf="@id/height"
        android:layout_alignBaseline="@id/height"/>
</RelativeLayout>

2.java文件如下:
MainActivity.java:

package com.example.simplepreferencedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText nameText;
    private EditText ageText;
    private EditText heightText;

    public static final String PREFERENCE_NAME="SaveSetting";
    public static int MODE=  Context.MODE_PRIVATE + Context.MODE_PRIVATE;

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

        nameText=(EditText)findViewById(R.id.name);
        ageText=(EditText)findViewById(R.id.age);
        heightText=(EditText)findViewById(R.id.height);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadSharedPreferences();
    }

    @Override
    protected void onStop() {
        super.onStop();
        saveSharedPreferences();
    }

    private void loadSharedPreferences(){
        SharedPreferences sharedPreferences=getSharedPreferences(PREFERENCE_NAME,MODE);
        String name=sharedPreferences.getString("Name","Tom");
        int age=sharedPreferences.getInt("Age",20);
        float height=sharedPreferences.getFloat("Height",1.81f);

        nameText.setText(name);
        ageText
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值