先来看看程序运行图:
1.用户未输入状态:
2.用户单击记住密码:
3.用户单击读取密码:
接下来我们来看实现代码:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
//声明SharedPreferences对象。
SharedPreferences sharedPreferences;
//声明Editor对象。
SharedPreferences.Editor editor;
private Button remberButton;
private Button readButton;
private EditText studentNumber;
private EditText passWord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
remberButton=(Button)findViewById(R.id.remberButton);
readButton=(Button)findViewById(R.id.readButton);
studentNumber=(EditText)findViewById(R.id.studentNumber);
passWord=(EditText)findViewById(R.id.passWord);
//SharedPreferences本身是一个接口,程序无法直接创建实例因此只能通过Context提供的getSharedPreferences方法来获取SharedPreferences实例。
/*该方法的第二个参数有三个值分别为:
* 1.Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本程序访问。
* 2.Context.MODE_WORLD_READABLE:指定数据可被其他应用读取但不能写入。
* 3.Context.MODE_WORLD_WRITEABLE:指定数据可以被其他应用读写。
* 注意:
* 从Android 4.2开始Android官方并不再推荐使用Context.MODE_WORLD_READABLE,Context.MODE_WORLD_WRITEABLE
* 这两种方式因为这两种方式允许外来应用访问他人的软件会造成安全漏洞。
*
*
* */
sharedPreferences=getSharedPreferences("studentList", Context.MODE_PRIVATE);
editor=sharedPreferences.edit();
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//使用sharedPreferences.getString()的方法来访问保存的数据。
String studentNumber=sharedPreferences.getString("studentNumber",null);
String passWord=sharedPreferences.getString("passWord",null);
if(studentNumber==null)
{
Toast.makeText(MainActivity.this,"当前没有学生记录请添加!",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(MainActivity.this,"学号:"+studentNumber+"\n"+"密码:"+passWord,Toast.LENGTH_LONG).show();
}
}
});
remberButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//写入数据使用editor.putXxx()这里可以指定写入的数据类型。
editor.putString("studentNumber",studentNumber.getText().toString());
editor.putString("passWord",passWord.getText().toString());
//提交数据。
editor.commit();
Toast.makeText(MainActivity.this,"成功记录密码!!!",Toast.LENGTH_SHORT).show();
}
});
}
}
下面是布局代码:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zhangyi.sharedpreferences_test.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:id="@+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/studentNumber"
android:hint="请输入学号..."
android:layout_alignBottom="@+id/textView"
android:layout_toEndOf="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="45dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:hint="请输入密码..."
android:id="@+id/passWord"
android:layout_alignBottom="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/remberButton"
android:layout_below="@+id/passWord"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取密码"
android:id="@+id/readButton"
android:layout_below="@+id/remberButton"
android:layout_alignStart="@+id/remberButton"
android:layout_marginTop="39dp" />
</RelativeLayout>
本文介绍了如何在Android应用中使用SharedPreferences来保存登录时的账号和密码,包括用户未输入、记住密码和读取密码的功能实现,通过代码展示了具体操作步骤。
3112

被折叠的 条评论
为什么被折叠?



