sharepreference存储数据

activity界面显示

package edu.hrbeu.SimplePreferenceDemo;

import edu.hrbue.SimplePreferenceDemo.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SimplePreferenceDemoActivity extends Activity  implements OnClickListener{

    private EditText nameText;
    private EditText ageText;
    private EditText heightText;
    private Button btnSave,btnRead;
    private TextView tvShow;

    public static final String PREFERENCE_NAME = "SaveSetting";
    public static int MODE = Context.MODE_PRIVATE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        nameText = (EditText)findViewById(R.id.name);
        ageText = (EditText)findViewById(R.id.age);
        heightText = (EditText)findViewById(R.id.height);

        btnRead  = (Button)findViewById(R.id.btnRead);
        btnSave = (Button)findViewById(R.id.btnSave);
        btnRead.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        tvShow = (TextView)findViewById(R.id.showView);



    }

    @Override
    public void onStart(){
        super.onStart();

    }

    @Override
    public void onStop(){
        super.onStop(); 

    }

    private void loadSharedPreferences(){
        SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        String name = sharedPreferences.getString("Name","Tom");
        int age = sharedPreferences.getInt("Age", 20);
        float height = sharedPreferences.getFloat("Height",1.81f);

        String msg ="";
        msg+=name+" ,"+age+"  "+height+"\n";
        tvShow.setText(msg);
//          NAMETEXT.SETTEXT(NAME);
//          AGETEXT.SETTEXT(STRING.VALUEOF(AGE));
//          HEIGHTTEXT.SETTEXT(STRING.VALUEOF(HEIGHT));     
    }

    private void saveSharedPreferences(){
        SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("Name", nameText.getText().toString());
        editor.putInt("Age", Integer.parseInt(ageText.getText().toString()));
        editor.putFloat("Height", Float.parseFloat(heightText.getText().toString()));
        editor.commit();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.btnRead:
        {

            loadSharedPreferences();
            break;
        }

         case R.id.btnSave:
         {

            saveSharedPreferences();
            break;
         }
        default:
            break;
        }
    }


}

xml代码

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" >

        <EditText android:id="@+id/name"
            android:text=""  
            android:layout_width="280dip" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:layout_marginLeft="10dip" >
        </EditText>
        <TextView android:id="@+id/name_label"
            android:text="姓名:"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" 
            android:layout_toRightOf="@id/name" 
            android:layout_alignBaseline="@+id/name">
        </TextView>

        <EditText android:id="@+id/age"
            android:text=""  
            android:layout_width="280dip" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:layout_marginLeft="10dip" 
            android:layout_below="@id/name" 
            android:numeric="integer">
        </EditText>
        <TextView android:id="@+id/age_label"
            android:text="年龄:"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" 
            android:layout_toRightOf="@id/age" 
            android:layout_alignBaseline="@+id/age" >
        </TextView>

        <EditText android:id="@+id/height"
            android:layout_width="280dip" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:layout_marginLeft="10dip"
            android:layout_below="@id/age" 
            android:numeric="decimal">
        </EditText>
        <TextView android:id="@+id/height_label"
            android:text="身高:"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" 
            android:layout_toRightOf="@id/height" 
            android:layout_alignBaseline="@+id/height">
        </TextView>

        <Button
            android:id="@+id/btnSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/height"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="80dp"
            android:text="保存" />
        <TextView
            android:id="@+id/showView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/btnSave"
            android:layout_below="@+id/btnSave"
            android:layout_marginTop="50dp"
            android:text="" />

        <Button
            android:id="@+id/btnRead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btnSave"
            android:layout_alignBottom="@+id/btnSave"
            android:layout_marginLeft="32dp"
            android:layout_toRightOf="@+id/btnSave"
            android:text="读取" />

    </RelativeLayout>

效果显示
这里写图片描述

发布了42 篇原创文章 · 获赞 24 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/zhang245754954/article/details/53785801