通过实例学Android应用开发11

大家如果是要跟着我文章来实践的话,还是从头看到尾,依据已经给出的截图及实现过程先自行完成,源码贴在最后,各有各的写法,只要能实现都是了不起的。加油吧,一起努力!

实例

题目

使用SharePreferences保存用户登录信息。

程序结果展示界面

实现效果

程序首次运行时输入电话号码和城市名称并退出后,再次进入时便能显示上次输入的用户信息。

结果页面

在这里插入图片描述
在这里插入图片描述

涉及到的知识点

SharedPreferences 接口

      为了方便用户操作,可以将用户名及密码保存至某个文件里,供下一次登录时使用,节省用户输入时间,可以使用Android提供的SharedPreferences接口。用户信息保存在工程名包下的shared_prefs文件夹里的一个.xml文件里,该文件保存在data/data/包名/shared_prefs里,以键值对的形式存放Activity里的文本框等的历史输入值。

      SharedPreferences接口位于软件包android.content里,每个Activity都有一个SharedPreferences接口类型的对象,该对象通过使用Activity父类(Context)提供的方法getSharedPreferences()得到。

      SharedPreferences接口提供了获取用户信息的方法getString()及编辑用户信息的edit()方法。edit()方法的返回值为SharedPreferences接口的内部接口类型Editor。内部接口Editor提供了以键值对形式保存用户信息的方法putString()和提交用户信息方法commit()

      Android应用程序默认安装至手机内存里(即手机ROM,而不是RAM或者SD卡),项目SharedPreferencesDemo会生成隶属于该应用的.xml文件,存放在系统文件夹data/data/pn/shared_prefs里,通过DDMS的File Explorer(文件浏览器)可以查看。

注意:Android应用程序安装至手机不是AVD,生成的数据文件由于权限不够而不能查看,除非手机已经Root了。

实现过程

  1. 新建名为SharedPreferencesDemo的应用工程
  2. 修改默认布局文件activity_main.xml,采用垂直线性布局,包含三个TextView控件和两个EditText控件
  3. 编写程序MainActivity.java

源码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用SharedPreferences存储程序信息"
        android:textSize="20sp"
        android:gravity="center_horizontal"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="您的电话号码:"
        />
    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入电话号码"
        android:textSize="22sp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="您所在的城市:"
        />
    <EditText
        android:id="@+id/city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入城市名称"
        android:textSize="22sp"/>

</LinearLayout>

MainActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    

    private EditText phoneText,cityText;
    private SharedPreferences sp;
    private String phone,city;
    private static final String PHONE="PHONE";
    private static final String CITY="CITY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneText = (EditText) this.findViewById(R.id.phone);
        cityText = (EditText) this.findViewById(R.id.city);
        sp = this.getPreferences(Activity.MODE_PRIVATE);
        //取出保存的电话号码和地址信息
        phone = sp.getString(PHONE,null);
        city = sp.getString(CITY,null);

        //将取出的信息分别放在对应的EditText控件中
        phoneText.setText(phone);
        cityText.setText(city);
    }

    @Override
    protected void onStop(){
    
    
        sp.edit()
            .putString(PHONE,phoneText.getText().toString())
            .putString(CITY,cityText.getText().toString())
            .commit();
        super.onStop();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51229662/article/details/124545439