Android之SharedPreferences的基本用法

本质:通过Key-value(键值对)来存储一些轻量级的数据,一些配置信息,如用户名、密码、姓名、电话等等之类的注册性信息。

 

保存数据的用法

1.实例化SharedPreference对象,第一个参数是保存的文件名,第二个参数是保存的类型。

SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);

2.实例化Editor对象。

Editor editor=sp.edit();

3.通过实例化的Editor对象调用putString方法来保存数据。

editor.putString("account", zh);

editor.putString("password", mm);

4.提交保存的数据

  editor.commit();

 

读取数据的方法

1.实例化SharedPreference对象,可以使用保存时创建的对象。

2.使用getString方法获得String类型的value,还有其他get方法,如getLonggetInt等等主要看你保存的数据是什么类型。注:第二个参数默认为null

   String account=sp.getString("account"null);

   String password=sp.getString("password"null);


下面举一个实例来理解:

MainActivity中的代码:

public class MainActivity extends Activity implements OnClickListener{

private EditText account;

private EditText password;

private Button save;

private Button read;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

account=(EditText)findViewById(R.id.account);

password=(EditText)findViewById(R.id.password);

save=(Button)findViewById(R.id.save);

read=(Button)findViewById(R.id.read);

save.setOnClickListener(this);

read.setOnClickListener(this);

}

@Override

public void onClick(View v) {

//通过getText()方法得到输入的用户名和密码

String zh=account.getText().toString().trim();

String mm=password.getText().toString().trim();

//保存的文件名是info,保存类型是私有的

SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);

switch (v.getId()) {

case R.id.save:

Editor editor=sp.edit();

editor.putString("account", zh);

editor.putString("password", mm);

editor.commit();

Toast.makeText(this"保存成功",Toast.LENGTH_SHORT).show();

break;

case R.id.read:

String account=sp.getString("account"null);

String password=sp.getString("password"null);

Toast.makeText(this"读取数据如下:"+"\n"+"用户名:" + account + "\n" + "密码:" + password, 

Toast.LENGTH_LONG).show(); 

  default:

break;

}

}

 

 

}


布局文件中的代码:

<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="#87ceeb"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"

        android:orientation="horizontal" >

 

        <TextView

            android:paddingLeft="20dp"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="用户名:"

            android:textSize="20sp" />

 

        <EditText

            android:id="@+id/account"

            android:layout_width="200dp"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            android:background="@drawable/round" />

    </LinearLayout>

 

     <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"

        android:orientation="horizontal" >

 

        <TextView

            android:paddingLeft="20dp"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="密    码:"

            android:textSize="20sp" />

 

        <EditText

            android:inputType="textPassword"

            android:id="@+id/password"

            android:layout_width="200dp"

            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"

            android:background="@drawable/round"

            android:ems="10" />

 

    </LinearLayout>

    <LinearLayout 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:layout_marginTop="10dp">

        

        <Button

            android:id="@+id/save"

            android:layout_marginLeft="20dp"

            android:background="@drawable/btn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="保存数据" 

            />

        <Button 

            android:id="@+id/read"

            android:layout_marginLeft="20dp"

            android:background="@drawable/btn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="读取数据"/>

       

        </LinearLayout>

</LinearLayout>


自定义的关于EditTextbuttonxml文件代码:作用是修饰各自的边框

这个是关于EditText

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <solid android:color="#FFFFFF"/>

    <stroke android:color="#000000" />

    <padding android:left="10dp"

        ></padding>

    <corners android:radius="10dp"></corners>

</shape>

 

这个是关于Button

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

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >     

     <item >

         <shape >

            <corners android:radius="10dp" />

            <gradient android:startColor="#add8e6" 

                 android:endColor="#00008b"

                 android:angle="-90"/>

         </shape>        

      </item>    

 </selector>


效果图:





效果图:文件被保存到的地方



效果图:sharedpreferences保存的文件格式是xml文件格式


 

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值