Android Application Development by Example 11

sequence

If you want to follow my article to practice, you should read from the beginning to the end, and complete it by yourself according to the screenshots and implementation process given. The source code is pasted at the end. Each has its own way of writing. Come on, let's work hard together!

example

topic

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

Program result display interface

achieve effect

When the program runs for the first time, enter the phone number and city name and exit, and the user information entered last time will be displayed when you enter again.

results page

insert image description here
insert image description here

Knowledge points involved

SharedPreferences interface

      In order to facilitate the user's operation, the user name and password can be saved in a file for the next login, saving the user's input time, and the SharedPreferences interface provided by Android can be used. User information is stored in an .xml file in the shared_prefs folder under the project name package. This file is stored in the file, and data/data/包名/shared_prefsstores historical input values ​​such as text boxes in the Activity in the form of key-value pairs.

      The SharedPreferences interface is located in the software package android.content, and each Activity has an object of the SharedPreferences interface type, which is getSharedPreferences()obtained by using the method provided by the Activity parent class (Context).

      The SharedPreferences interface provides methods for obtaining user information getString()and editing user information edit(). edit()The return value of the method is the internal interface type Editor of the SharedPreferences interface. The internal interface Editor provides the method of saving user information in the form of key-value pairs putString()and the method of submitting user information commit().

      The Android application is installed in the mobile phone memory by default (that is, the mobile phone ROM, not the RAM or SD card). The project SharedPreferencesDemo will generate an .xml file belonging to the application and store it in the system folder data/data/pn/shared_prefs. Through DDMS File Explorer (file browser) can view.

Note: The Android application installed on the mobile phone is not an AVD, and the generated data files cannot be viewed due to insufficient permissions, unless the mobile phone is rooted.

Implementation process

  1. Create a new SharedPreferencesDemoapplication project named
  2. Modify the default layout file activity_main.xmlto adopt a vertical linear layout, including three TextView controls and two EditText controls
  3. ProgrammingMainActivity.java

source code

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();
    }
}

Guess you like

Origin blog.csdn.net/weixin_51229662/article/details/124545439