zoukankan      html  css  js  c++  java
  • Android中SharePreferences的简单实现

    Android中提供SharePreferences这种轻量级的数据存储模式,这种模式能够存储少量数据,并能为自身和其他应用提供数据接口。相对于其他数据存储方式,SharePreferences更加轻量。以下是整个SharePreferences实现的代码:

    xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText" android:width="200dp"/>
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:ems="10"
                android:id="@+id/editText2"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="保存"
                android:id="@+id/button"/>
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView"/>
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView2"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示"
                android:id="@+id/button2"/>
    </LinearLayout>

    java主文件:

    package com.example.sharepreferencesTest;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
        private EditText user;
        private EditText address;
        private Button login;
        private Button show;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            user = (EditText)findViewById(R.id.editText);
            address = (EditText)findViewById(R.id.editText2);
            login = (Button)findViewById(R.id.button);
    
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //写入SharedPreferences中
                    SharedPreferences user_ino = getSharedPreferences("user_ino", Context.MODE_PRIVATE);  //使用MODE_PRIVATE模式
                    SharedPreferences.Editor editor = user_ino.edit();
    
                    String ename = user.getText().toString();
                    String eaddress = address.getText().toString();
    
                    editor.putString("name",ename);
                    editor.putString("address", eaddress);
                    editor.commit();
                }
            });
    
            show = (Button)findViewById(R.id.button2);
            show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //读入SharedPreferences中的值
                    SharedPreferences user_ino2 = getSharedPreferences("user_ino", Context.MODE_PRIVATE);
                    String name1 = user_ino2.getString("name","");
                    String age1 = user_ino2.getString("address","");
    
                    //显示出读出的值
                    TextView tv1 = (TextView)findViewById(R.id.textView);
                    TextView tv2 = (TextView)findViewById(R.id.textView2);
                    tv1.setText(name1);
                    tv2.setText(age1);
                }
            });
    
    
    
    
    
        }
    }
  • 相关阅读:
    毛笔算法 毛笔签名效果
    手写输入控件
    全栈工程师成长路线
    配置msdtc
    流行的广告轮播(图片轮播)JS代码!!
    水晶报表放上去网站会爆:bobj错误的
    查询所有存储过程
    VB.NET and C# Comparison
    SQL查询案例:多行转换为一行
    SQL Server行列转换[转]
  • 原文地址:https://www.cnblogs.com/starwolf/p/3739327.html
Copyright © 2011-2022 走看看