[Experiment] The mobile terminal Application Development 1: SharedPreferences Application

Disclaimer: This article is a blogger original article. Reproduced please contact bloggers authorization. Bloggers public micro-channel number] [Department of Knowing and Doing the campus. https://blog.csdn.net/cxh_1231/article/details/84454016

First, the purpose of the experiment:

  1. Familiar development environment and the development of Android applications;
  2. Familiar with the basic framework of Android applications;
  3. Master the creation and use of Activity component;
  4. Mastering Android layout and basic management controls;
  5. Familiar Android data storage scheme, master SharedPreferences to use.

Second, the experimental content:

  1. Create a new Android application, design an input interface information of the user's name, password, phone, mail and gender.
  2. Add interfacial "Save", "read" and "clear" button, etc., and the design response code button. "Save" button interface input information stored in the shared file SharedPrefences; "read" button on the interface displayed on the read information from the shared file. "Clear" button to clear the screen information.
  3. Interface information is automatically saved to a shared folder when you quit the application or shut down.

Third, the experimental guidance:

  • Create an Android application, design a GUI interface, the effect shown in Figure 1.

  • In FIG 1 Spinner control achieved using the drop-down list. Use this control is similar to the ListView control, we need a Adapte object when loading data, and in the process create Adapter object of the specified data (array or List object) to load. The key code is as follows:
private String[] gender_list = new String[]{"男", "女"}; 
Spinner sp_gender = (Spinner) findViewById(R.id.sp_gengder); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.spinner_item, gender_list); 
sp_gender.setAdapter(adapter); 
//获取下拉列表当前列项的内容 
String user_gender = (String)sp_gender.getSelectedItem();
  • SharedPreferences store data to the basic steps:
    (1) using getSharedPreferences Context class (String name, int mode) SharedPreferences method for obtaining an object;
    (2) SharedPreferences object edit () method to obtain a SharedPreferences.Editor object.
    (3) using SharedPreferences.Editor object putXxx () method (e.g. putString (), putInt (), etc.) to add data to SharedPreferences.Editor object.
    (4) calls apply () method to add the data submitted.
    E.g:
  • SharedPreferences pref = getSharedPreferences("user_info", MODE_PRIVATE); 
    SharedPreferences.Editor editor = pref.edit(); 
    editor.putString("user_name", "Tom"); // 存入数据 
    editor.apply(); // 提交修改

     

  • Reading data from SharedPreferences method
    (1) using getSharedPreferences Context class (String name, int mode) obtained SharedPreferences object.
    (2) the object using SharedPreferences getXxx () method (e.g. getString (), getInt (), etc.) to read data from the SharedPreferences.
    E.g:
SharedPreferences pref = getSharedPreferences("user_info", MODE_PRIVATE); 
String userName = pref.getString("user_name", ""); //获取数据

Fourth, the process of recording:

1, activity_main.xml layout design:

XML File Layout (Click image to view larger image)

2, activity_main Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/UserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="38dp"
        android:layout_toStartOf="@+id/choosesex"
        android:text="用户名:"
        android:textSize="50px" />

    <EditText
        android:id="@+id/editUserName"
        android:layout_width="263dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/UserName"
        android:layout_toEndOf="@+id/UserName"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <TextView
        android:id="@+id/PassWord"
        android:layout_width="93dp"
        android:layout_height="33dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/UserName"
        android:layout_marginTop="103dp"
        android:text="密 码:"
        android:textSize="50px" />

    <TextView
        android:id="@+id/Tel"
        android:layout_width="92dp"
        android:layout_height="33dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/UserName"
        android:layout_marginTop="162dp"
        android:text="电 话:"
        android:textSize="50px" />

    <TextView
        android:id="@+id/Email"
        android:layout_width="92dp"
        android:layout_height="31dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/UserName"
        android:layout_marginTop="223dp"
        android:text="邮 箱:"
        android:textSize="50px" />

    <TextView
        android:id="@+id/Sex"
        android:layout_width="93dp"
        android:layout_height="38dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/UserName"
        android:layout_marginBottom="254dp"
        android:text="性 别:"
        android:textSize="50px" />

    <EditText
        android:id="@+id/editEmail"
        android:layout_width="263dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Email"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="14dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="262dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/PassWord"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="14dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <EditText
        android:id="@+id/editTel"
        android:layout_width="264dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Tel"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="14dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <Spinner
        android:id="@+id/choosesex"
        android:layout_width="236dp"
        android:layout_height="36dp"
        android:layout_alignBottom="@+id/Sex"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="39dp" />

    <Button
        android:id="@+id/buttonsave"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="178dp"
        android:onClick="buttonsave_Click"
        android:text="保 存" />

    <Button
        android:id="@+id/buttonread"
        android:layout_width="322dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="122dp"
        android:onClick="buttonread__Click"
        android:text="读 取" />

    <Button
        android:id="@+id/buttonblank"
        android:layout_width="325dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="66dp"
        android:onClick="buttonclean__Click"
        android:text="清 空" />

</RelativeLayout>

3, MainActivity.java file code:

package com.cxhit.test1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.EditText;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText getUserName,getUserPassword,getUserTel,getUserEmail;
    private Spinner getUserSex;
    private Button saveButton,readButton,cleanButton;

    ////声明Sharedpreferenced对象
    private SharedPreferences sp;

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

        Spinner spinner = (Spinner) findViewById(R.id.choosesex);
        final List<String> datas = new ArrayList<>();
       datas.add("男");
       datas.add("女");
        MyAdapter adapter = new MyAdapter(this);
        spinner.setAdapter(adapter);
        adapter.setDatas(datas);
        
        saveButton = (Button) findViewById(R.id.buttonsave);
        readButton = (Button) findViewById(R.id.buttonsave);
        cleanButton = (Button) findViewById(R.id.buttonblank);

    }
    public void buttonsave_Click(View view) {
        getUserName = (EditText) findViewById(R.id.editUserName);
        getUserPassword = (EditText) findViewById(R.id.editPassword);
        getUserTel = (EditText) findViewById(R.id.editTel);
        getUserEmail = (EditText) findViewById(R.id.editEmail);
        getUserSex = (Spinner) findViewById(R.id.choosesex);
        
        SharedPreferences pref = getSharedPreferences("user_info",MODE_PRIVATE);
        SharedPreferences.Editor editor =pref.edit();
        editor.putString("username",getUserName.getText().toString());
        editor.putString("userpassword",getUserPassword.getText().toString());
        editor.putString("usertel",getUserTel.getText().toString());
        editor.putString("useremail",getUserEmail.getText().toString());
        editor.putString("usersex",getUserSex.getSelectedItem().toString());
        editor.apply();
        Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_LONG).show();
    }

    public void buttonread__Click(View view)
    {
        getUserName = (EditText) findViewById(R.id.editUserName);
        getUserPassword = (EditText) findViewById(R.id.editPassword);
        getUserTel = (EditText) findViewById(R.id.editTel);
        getUserEmail = (EditText) findViewById(R.id.editEmail);
        getUserSex = (Spinner) findViewById(R.id.choosesex);

        SharedPreferences pref = getSharedPreferences("user_info",MODE_PRIVATE);
        getUserName.setText(pref.getString("username",""));
        getUserPassword.setText(pref.getString("userpassword",""));
        getUserTel.setText(pref.getString("usertel",""));
        getUserEmail.setText(pref.getString("useremail",""));
        SpinnerAdapter spAdapter = getUserSex.getAdapter();
        int k = spAdapter.getCount();
        for (int i=0;i<k;i++)
        {
            if(pref.getString("usersex","").equals(spAdapter.getItem(i)))
            {
                getUserSex.setSelection(i);
                break;
            }
        }
        Toast.makeText(MainActivity.this,"读取成功",Toast.LENGTH_LONG).show();
    }

    public void buttonclean__Click(View view)
    {
        getUserName = (EditText) findViewById(R.id.editUserName);
        getUserPassword = (EditText) findViewById(R.id.editPassword);
        getUserTel = (EditText) findViewById(R.id.editTel);
        getUserEmail = (EditText) findViewById(R.id.editEmail);
        getUserSex = (Spinner) findViewById(R.id.choosesex);

        getUserSex.setSelection(0);
        getUserName.setText("");
        getUserPassword.setText("");
        getUserEmail.setText("");
        getUserTel.setText("");
        Toast.makeText(MainActivity.this,"清空成功",Toast.LENGTH_LONG).show();
    }
}

6.3.5 Operating results:

Sixth, written in the last:

Tonight just finished mobile terminal application development, this piece of paper was issued to.

In fact, the two experiments Android development is not difficult, but also the basics of Android must zhan grasp. On examination of tonight two programming problem is this experiment two questions, namely the use of SharedPreferences and SQLitebaoc save TestView enter the user name and password.

This article is for reference only.

Guess you like

Origin blog.csdn.net/cxh_1231/article/details/84454016