android 一个SharedPreferences用法的例子

来源:互联网 发布:云南大学软件学院图片 编辑:程序博客网 时间:2024/06/03 21:15

用登陆的小例子看看SharedPreferences怎么用,首先来简单介绍一下功能实现:

登陆界面

喏,这是登陆界面啦,输入用户名密码,然后……登陆,大家可以看到有一个记住密码的CheckBox哈,勾选它,注意啦,SharedPreferences就是在这里用到的,因为你的勾选,哈哈~~输入的用户名密码就会生成一个文件如下:info.xml

info.xml

这样子呢,你下一次登录就不用再输入用户名和密码啦!好了,现在我们来看一下代码是如何实现的!

首先,布局文件:login.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/et_username"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/username" >        <requestFocus />    </EditText>    <EditText        android:id="@+id/et_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/password"        android:inputType="textPassword" />    <CheckBox        android:id="@+id/cb_savePassword"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/savePassword" />    <Button        android:id="@+id/b_login"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/login" /></LinearLayout>

然后是Activity:SharedPrefrence_Aty.java

import com.example.practice.R;import com.example.practice.service.LoginService;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class SharedPrefrence_Aty extends Activity implements        OnClickListener {    EditText et_username;    EditText et_password;    CheckBox cb_savePassword;    Button b_login;    SharedPreferences preferences;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.login);        et_username = (EditText) findViewById(R.id.et_username);        et_password = (EditText) findViewById(R.id.et_password);        cb_savePassword = (CheckBox) findViewById(R.id.cb_savePassword);        b_login = (Button) findViewById(R.id.b_login);        preferences = getSharedPreferences("info", Context.MODE_PRIVATE);        String username = preferences.getString("username", "");        String password = preferences.getString("password", "");        et_username.setText(username);        et_password.setText(password);        cb_savePassword.setChecked(true);        b_login.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {        case R.id.b_login:            getLoginInfo();            break;        }    }    /**     * 获取登录信息并验证     */    private void getLoginInfo() {        // TODO Auto-generated method stub        String usernameStr = et_username.getText().toString().trim();        String passwordStr = et_password.getText().toString().trim();        if (TextUtils.isEmpty(usernameStr) || TextUtils.isEmpty(passwordStr)) {            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();        } else {            if (cb_savePassword.isChecked()) {                LoginService.saveUserInfo2(this, usernameStr, passwordStr);            }            if (usernameStr.equals("guai") && passwordStr.equals("123")) {                Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();            }        }    }}

LoginService.java

import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class LoginService {    public static void saveUserInfo2(Context context, String username,            String password) {        // TODO Auto-generated method stub        SharedPreferences preferences = context.getSharedPreferences("info",                Context.MODE_PRIVATE);        Editor editor = preferences.edit();        editor.putString("username", username);        editor.putString("password", password);        editor.commit();    }}

好了,以上就是这个小例子的全部代码,其实,SharedPreferences的用法主要在LoginService.java这几行代码里,其他都是浮云。

0 0
原创粉丝点击