Android之SharedPreference

// by pbImage
// 2012-03-26
package com.pbAndroid.SharePref;

import android.app.Activity;
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 pbSharedPref extends Activity {
	
	EditText edit_userName = null;
	EditText edit_passWord = null;
	Button button_Save = null;
	Button button_Open = null;
	
	String userName = null;
	String passWord = null;
	
	String get_userName = null;
	String get_passWord = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        edit_userName = (EditText)findViewById(R.id.Edit_userName);
        edit_passWord = (EditText)findViewById(R.id.Edit_passWord);
        
        button_Save = (Button)findViewById(R.id.Button_Save);
        button_Open = (Button)findViewById(R.id.Button_Open);

        OnClickListener saveListener = new View.OnClickListener() {  //保存事件监听
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);
				Editor pbEditor = pbSharePref.edit();
				
				userName = edit_userName.getText().toString();
				passWord = edit_passWord.getText().toString();
				
				pbEditor.putString("save_userName", userName);
				pbEditor.putString("save_passWord", passWord);
				pbEditor.commit();
				Toast.makeText(pbSharedPref.this, "save success!", Toast.LENGTH_SHORT).show();
			}
		};
		OnClickListener openListener = new View.OnClickListener() {  //打开事件监听
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);
				
				get_userName = pbSharePref.getString("save_userName", "");
				get_passWord = pbSharePref.getString("save_passWord", "");
				
				Toast.makeText(pbSharedPref.this, "帐号: " + get_userName + " 密码: " + get_passWord , Toast.LENGTH_LONG).show();
			}
		};
		
		button_Save.setOnClickListener(saveListener);
		button_Open.setOnClickListener(openListener);
    }
}


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/label_userName" />
    <EditText
        android:hint=""
        android:id="@+id/Edit_userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/label_passWord"/>
    <EditText 
        android:hint=""
        android:id="@+id/Edit_passWord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:password="true"/>
    <Button 
        android:id="@+id/Button_Save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_Save"/>
    <Button 
        android:id="@+id/Button_Open"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_Open"/>
</LinearLayout>


//>>>>>>>>>>>>>>>>>>>>>结果>>>>>>>>>>>>>>>>>>>

                                           图1 点击【保存>>】

                                               图2 点击【打开>>】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值