Android-SharedPreferences的简单用法

2017-4-28小组作业SharedPreferences的简单用法

作业要求:使用SharedPreferences将姓名和年龄信息保存到文件,然后再读取。
首先是应用的界面布局,没什么好说的,都是一些常用的控件,代码如下:

<?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:orientation="vertical"
    tools:context="com.example.iowork.MainActivity">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="25sp"
        android:gravity="center"
        android:text="姓名:"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="0dp"
        android:textSize="25sp"
        android:layout_height="wrap_content"
        android:layout_weight="4"/>
</LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:gravity="center"
            android:text="密码:"/>
        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:textSize="25sp"
            android:layout_height="wrap_content"
            android:layout_weight="4"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/bt_write"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="写入"
            android:onClick="onClick"/>

        <Button
            android:id="@+id/bt_read"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="读取"
            android:onClick="onClick"/>
    </LinearLayout>

</LinearLayout>

功能实现使用的是SharedPreferences类来进行数据的写入和读取,下面是实现的具体代码:

package com.example.iowork;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_write:
                String name = et_name.getText().toString();
                String password = et_password.getText().toString();
                if(saveToPrefs(password,name)){
                    Toast.makeText(MainActivity.this,"写入成功",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt_read:
                readFromPrefs();
                break;
        }
    }
    //读取数据的方法
    private void readFromPrefs() {
        SharedPreferences preferences = getSharedPreferences("customer.txt",MODE_PRIVATE);
        String name=preferences.getString("name","");
        String password=preferences.getString("password","");
        et_password.setText(password);
        et_name.setText(name);
    }
    //写入数据的方法
    private boolean saveToPrefs(String password, String name) {
        SharedPreferences preferences = MainActivity.this.getSharedPreferences("customer.txt",MODE_PRIVATE);
        SharedPreferences.Editor editor =preferences.edit();
        editor.putString("name",name);
        editor.putString("password",password);
        editor.commit();
        return true;
    }
}

下面是应用运行截图:
1126725-20170509170218910-2050769850.png
1126725-20170509170240035-708302938.png

转载于:https://www.cnblogs.com/wang-Jeft/p/6831489.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值