[Android] SharedPreferences storage

.java code is as follows:

package org.lxh.demo;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private static final String FILENAME="mldn";	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		
		SharedPreferences share=super.getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
		SharedPreferences.Editor edit=share.edit();
		edit.putString("author", "zhangyayun");
		edit.putInt("age", 30);
		edit.commit();
	}
}

The following information is read with a code
main.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/authorinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/ageinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
 
    
 
</LinearLayout>

java code as follows

package org.lxh.demo;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private static final String FILENAME = "mldn";
	private TextView authorinfo = null;
	private TextView ageinfo = null;
 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.authorinfo = (TextView) super.findViewById(R.id.authorinfo);
		this.ageinfo = (TextView) super.findViewById(R.id.ageinfo);
		SharedPreferences share = super.getSharedPreferences(FILENAME,
				Activity.MODE_PRIVATE);
		this.authorinfo.setText("作者:" + share.getString("author", "没有作者信息"));
		this.ageinfo.setText("年龄" + share.getInt("age", 0));
	}
}

Here Insert Picture Description

Published 73 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40110781/article/details/104933616