android 39 共享首选项

本文介绍了一个使用SharedPreferences存储和读取用户登录信息的Android应用示例。通过一个具体的Activity代码展示了如何使用SharedPreferences来保存用户的登录ID和密码,并在下次启动应用时自动填充这些信息。
共享首选项SharedPreferences:用于存储少量数据,大量数据则存入文件或者sd卡。以键值对保存数据。

 

 activity.java

复制代码
package com.sxt.day06_05;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText metId,metPwd;
    SharedPreferences mSp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        readData();//自动输入密码
    }
    private void readData() {
        mSp=getSharedPreferences("login", MODE_PRIVATE);
        String id=mSp.getString("id", "");
        String pwd=mSp.getString("pwd", "");
        metId.setText(id);
        metPwd.setText(pwd);
    }
    private void setListener() {
        findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String id=metId.getText().toString();
                if(TextUtils.isEmpty(id)){
                    metId.setError("编号不能为空");
                    return ;
                }
                String pwd=metPwd.getText().toString();
                if(TextUtils.isEmpty(pwd)){
                    metPwd.setError("密码不能为空");
                    return ;
                }
                mSp=getSharedPreferences("login", MODE_PRIVATE);//文件名为login(data/data/com.sxt.day06_05/shared_prefs/login.xml),MODE_PRIVATE表示私有的
                Editor editor = mSp.edit();//Editor是内部接口
                editor.putString("id", id);
                editor.putString("pwd", pwd);
                editor.commit();//存入首选项
                Toast.makeText(MainActivity.this, "登陆完成", 2000).show();
            }
        });
    }
    private void initView() {
        metId=(EditText) findViewById(R.id.etId);
        metPwd=(EditText) findViewById(R.id.etPwd);
    }
}
复制代码

页面:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆编号" />

        <EditText
            android:id="@+id/etId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="2-10个字符" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆密码" />

        <EditText
            android:id="@+id/etPwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:hint="2-10个字符" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <Button 
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:text="登陆"/>
        <Button 
            android:id="@+id/btnExit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="退出"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
        
    </LinearLayout>
</LinearLayout>
复制代码

 login.xml


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4889718.html,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值