Android 采用SharedPreferences读/写数据 【学习记录】

本文详细解析了Android应用中使用string.xml和main.xml进行资源字符串定义及界面布局设计的过程,包括如何通过Java代码引用这些资源,并实现用户输入信息的保存与恢复。

string.xml描述文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, SharedPreferencesTest!</string>
	<string name="app_name">SharedPreferencesTest</string>
	<string name="name">用户名</string>
	<string name="pwd">密码</string>
	<string name="saveBtn">保存账户</string>
	<string name="come">恢复数据</string>
</resources>

main.xml主界面文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/name"
    />
<EditText
	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/etName"
	/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/pwd"
    />
<!--为了便于演示将其设置为false-->  
<EditText
	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/etPwd"
    android:password="false"
	/>    
<Button
	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/saveBtn"
    android:text="@string/saveBtn"
	/>
<Button
	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/come"
    android:text="@string/come"
	/>
</LinearLayout>

住Activity  Java代码:
package com.android.danny.share;

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

public class SharedPreferencesTest extends Activity {
    
	EditText etName;
	EditText etPwd;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        etName = (EditText)findViewById(R.id.etName);
		etPwd = (EditText)findViewById(R.id.etPwd);
        
        Button saveBtn = (Button)findViewById(R.id.saveBtn);
        Button comeBtn = (Button)findViewById(R.id.come);
        
        //写入数据
        saveBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				String name = etName.getText().toString();
				String password = etPwd.getText().toString();
				
				SharedPreferences pref = getSharedPreferences("myTest", Context.MODE_APPEND);
				Editor  editor = pref.edit();
				editor.putString("name", name);
				editor.putString("password", password);
				editor.commit();
				//记得添加 show() 方法!!!!!!!!!!!!!!!!
				Toast.makeText(SharedPreferencesTest.this, "保存成功!", Toast.LENGTH_SHORT).show();
			}
		});
        //获取数据
        comeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				SharedPreferences pref = getSharedPreferences("myTest", Context.MODE_APPEND);
				String name = pref.getString("name", "");
				String password = pref.getString("password", "");
				
				etName.setText(name);
				etPwd.setText(password);
			}
		});
    }
}

转载于:https://my.oschina.net/mikola/blog/55398

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值