Table of contents
1. Overview of shared parameters
2. Use shared parameters to read and write files
Copy the background image to the drawable directory
Create a second interface class based on the Empty Activity template
Change the string resource file strings.xml
Open the layout resource file activity_first.xml and write the code:
Open the layout resource file activity_second.xml and enter the code:
Open the interface class FirstActivity and enter:
Open the interface class SecondActivity and enter:
Start the application to see the effect:
View the file where the data is saved
Save the personal information file to the local computer
theory:
1. Overview of shared parameters
Android provides a simple data storage method SharedPreferences [Shared Preferences], which is a lightweight data storage method used to store some simple configuration information, which is stored in an XML file in the form of key-value pairs.
2. Use shared parameters to read and write files
Use the getPreferences(name, mode) method of the Activity to get the SharedPreferences object
Use the edit() of the SharedPreferences object to get the Editor object
Use the putXxx() method of the Editor object to write data; use the getXxx() of the SharedPreferences object to read data
for writing Operation, use the commit() method of the Editor object to submit data to the specified file
Case presentation:
Empty Activity
Create an Android application based on a templateShareData
Copy the background image to drawable
the directory
MainActivity
Rename FirstActivity
the corresponding layout file actiivty_main.xml
toactivity_first.xml
Empty Activity
Create a second interface class based on a template
Change the string resource filestrings.xml
Specific code:
<resources> <string name="app_name">Shared parameter demonstration - read and write data</string> <string name="write_data">Write data</string> <string name="read_data">Read data< /string> <string name="jump_to_second">Jump to the second window</string> </resources>
Open the layout resource fileactivity_first.xml写入代码:
Specific code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background1" android:orientation="vertical" android:gravity="center" tools:context=".FirstActivity"> <Button android:id="@+id/btn_write_data" android:layout_width="200dp" android:layout_height="wrap_content" android:onClick="doWriteData" android:text="@string/write_data" android:textSize="20sp" /> <Button android:id="@+id/btn_jump_to_second" android:layout_width="200dp" android:layout_height="wrap_content" android:onClick="doJumpToSecond" android:text="@string/jump_to_second" android:enabled="false" android:textSize="20sp" /> </LinearLayout>
Open the layout resource fileactivity_second.xml输入代码:
Specific code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background2" android:gravity="center" android:orientation="vertical" tools:context=".SecondActivity"> <Button android:id="@+id/btn_read_date" android:layout_width="220dp" android:layout_height="wrap_content" android:text="@string/read_data" android:onClick="doReadData" android:textSize="20sp"/> <TextView android:id="@+id/tv_person_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="20sp"/> </LinearLayout>
open interface classFirstActivity输入:
Specific code:
package net.zyt.share_date; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle ; ; import android.widget.Toast; public class FirstActivity extends AppCompatActivity { private static final String NAME="person_info";//Configuration file interface private static final int MODE= Context.MODE_PRIVATE;//File access mode private SharedPreferences sp;// Shared parameter object private SharedPreferences.Editor editor;//Editor object @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Use the layout resource file to set the user interface setContentView(R.layout.activity_first); //Get the shared parameter object sp=getSharedPreferences(NAME,MODE); //Get the editor object editor= sp.edit(); } // Write data, button data click processing method public void doWriteData(View view){ editor.putString("name","Xiaohong"); editor.putString("gender","female"); editor.putInt(" age",50); editor.putString("hobby","reading, drawing, playing games"); // Submit data and write to the specified file if (editor.commit()) { Toast.makeText(this , "Congratulations, the data is successfully written to the file!", Toast.LENGTH_SHORT).show(); findViewById(R.id.btn_jump_to_second).setEnabled(true);//Make [jump to the second window] button available } else { ", Toast.LENGTH_SHORT).show(); } } //【Jump to the second window】Click event processing method public void doJumpToSecond (View view) { //Create the intent to jump to the second window Intent intent = new Intent( this,SecondActivity. class); //Start the second window by intent startActivity(intent); } }
open interface classSecondActivity输入:
Specific code:
package net.zyt.share_date; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.TextView ; import android.widget.Toast; public class SecondActivity extends AppCompatActivity { private static final String NAME="person_info";//Configuration file interface private static final int MODE= Context.MODE_PRIVATE;//File access mode private SharedPreferences sp;// Shared parameter object private TextView tvPersonInfo;//Personal information label @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Use the layout resource file to set the user interface setContentView(R.layout.activity_second); //Get the control instance through the control resource identifier tvPersonInfo=findViewById(R.id.tv_person_info); //Get the shared parameter object sp=getSharedPreferences(NAME,MODE); } //Read Data]Click event processing method public void doReadData(View view){ //Get file data through shared parameter object String name=sp.getString("name",""); String gender=sp.getString("gender", ""); int age=sp.getInt("age",0); String hobby=sp.getString("hobby",""); // Create personal information string builder StringBuilder builder=new StringBuilder(); builder.append("Name: "+name+"\n") .append("Gender"+gender+"\n") .append("age"+age+"\n") .append("hobby"+hobby+"\n"); // Get the personal information string String personInfo=builder.toString(); // Display the person by toast Information Toast.makeText(this,personInfo,Toast.LENGTH_SHORT).show(); // Display personal information in the label tvPersonInfo.setText(personInfo); } }
Start the application to see the effect:
View the file where the data is saved
Save the personal information file to the local computer
Open with Notepad: