Android移动应用开发 **Sharepreference数据存储**

第三篇:Sharepreference数据存储

实验目的
1.了解Sharepreference数据存储方式的特点
2.掌握Sharepreference数据存储的过程

实验任务及要求
在开发的过程中我们必须遇到的就是如何对用户的数据进行有效的存储以及读取。我们举个例子,现在我们使用app,当我们登陆一个账号的时候选择记住密码软件就会记住我们的账号以及密码,我们退出当前账号,就可以直接点击登陆进入账号内部,而不需要再输入账号和密码了。那么这就是今天我们要做的,如何对用户输入的账号以及密码进行存储,并且进行显示。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
实验步骤
1.创建布局文件“activity_main.xml”。
2.编写Login service.java。
3. 编写activity_main.java
4.运行程序并验证。
程序代码:
MainActivity.java代码:

public class MainActivity extends Activity {

	private static final String Tag = "MainActivity";
	public EditText ed_username;
	public EditText ed_pasw;
	public EditText ed_special;
	public CheckBox ch;
	public Button login;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ed_username=(EditText) findViewById(R.id.text_username);
		ed_pasw=(EditText) findViewById(R.id.text_pas);
		ed_special=(EditText) findViewById(R.id.tv_special);
		ed_special.setEnabled(false);
		ch=(CheckBox) findViewById(R.id.ch);
		login=(Button) findViewById(R.id.btn_login);

		//显示用户此前录入的数据
		SharedPreferences sPreferences=getSharedPreferences("config", MODE_PRIVATE);
		String username=sPreferences.getString("username", "");
		String password =sPreferences.getString("password", "");
		String specialText=sPreferences.getString("specialtext", "");
		ed_username.setText(username);
		ed_pasw.setText(password);
		ed_special.setText(specialText);
	}
	/**
	 * 登陆
	 * @param view
	 */
	public void login(View view){
		String username=ed_username.getText().toString().trim();
		String pasw=ed_pasw.getText().toString().trim();
		if (TextUtils.isEmpty(username)||TextUtils.isEmpty(pasw)) {
			Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
		}else {
			//判断是否保存密码
			if (ch.isChecked()) {
				Log.i(Tag, "用户需要保存密码");
				Loginservice.saveUserInfo(this,username, pasw);
				Toast.makeText(this, "保存用户数据成功", Toast.LENGTH_SHORT).show();;

			}
			if (username.equals("aaa")&&pasw.equals("123")) {
				Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
			}
		}}}

activity_main.xml代码:

<TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:textSize="20sp" android:text="登录名:" 
        android:id="@+id/tv_ueername"/>
    <EditText 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:id="@+id/text_username"/>
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:textSize="20sp" android:text="密码:" 
        android:id="@+id/tv_pas"/>
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/text_pas" android:inputType="textPassword"/>
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
android:textSize="20sp" 
android:text="特殊字符:" 
android:id="@+id/tv_special"/>
<EditText 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:id="@+id/text_special"/>
<RelativeLayout 
android:layout_height="wrap_content" 
android:layout_width="fill_parent">
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" 
android:text="记住密码" android:id="@+id/ch" 
android:checked="true"/>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" 
android:text="登陆" 
android:id="@+id/btn_login" 
android:layout_alignParentRight="true" 
android:onClick="login"/>
</RelativeLayout>

Login service.java代码:

public class Loginiservice {
	/**
	 * 保存用户名 密码的业务方法
	 * @param context 上下文
	 * @param username 用户名
	 * @param pas 密码
	 * @return true 保存成功  false 保存失败
	 */
	public static void saveUserInfo(Context context,String username,String pas){
		/**
		 * SharedPreferences将用户的数据存储到该包下的shared_prefs/config.xml文件中,
		 * 并且设置该文件的读取方式为私有,即只有该软件自身可以访问该文件
		 */
		SharedPreferences sPreferences=context.getSharedPreferences("config", context.MODE_PRIVATE);
		Editor editor=sPreferences.edit();
		//当然sharepreference会对一些特殊的字符进行转义,使得读取的时候更加准确
		editor.putString("username", username);
		editor.putString("password", pas);
		//这里我们输入一些特殊的字符来实验效果
		editor.putString("specialtext", "hajsdh><?//");
		editor.putBoolean("or", true);
		editor.putInt("int", 47);
		//切记最后要使用commit方法将数据写入文件
		editor.commit();
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值