Android笔记 SharedPreferences demo

1布局

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用户名" />

    <EditText
        android:id="@+id/etusername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用密码" />

    <EditText
        android:id="@+id/etpass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb_rem_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="记住密码" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="登陆" />
    </RelativeLayout>

</LinearLayout>

2代码

MainActivity.java

package com.example.a24_logform;


import com.example.a24_logform.lognservice.LoginService;
import com.example.a28_sharedpreference.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private static final String TAG="MainActivity";
	EditText editname =null;
	EditText editpass=null;
	CheckBox checkBox=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editname=(EditText) this.findViewById(R.id.etusername);
		editpass=(EditText) this.findViewById(R.id.etpass);
		checkBox=(CheckBox) this.findViewById(R.id.cb_rem_pass);
		
		SharedPreferences sp= getSharedPreferences("config", MODE_PRIVATE);
		String name=sp.getString("name", "");
		editname.setText(name);
		String pass=sp.getString("pass", "");
		editpass.setText(pass);
	}

	
	public void login(View view){
		String nameString=editname.getText().toString().trim();
		String passString=editpass.getText().toString().trim();
		if(TextUtils.isEmpty(nameString)||TextUtils.isEmpty(passString)){
			Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
		}else{
			if(checkBox.isChecked()){
				Log.i(TAG, "要保存密码");
				Toast.makeText(this, "记住密码", Toast.LENGTH_LONG).show();
				LoginService.saveUserInfo(this,nameString, passString);
			
					Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
				
			}
			if("zhangsan".equals(nameString)&&"123".equals(passString)){
				Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_LONG).show();
			}
		}
	}
}


LoginService.java

package com.example.a24_logform.lognservice;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class LoginService {
	public static void saveUserInfo(Context context, String name,String pass){
		SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);
		Editor editor= sp.edit();
		editor.putString("name", name);
		editor.putString("pass", pass);
		//采用类似事物的回滚 保证数据同时提交
		editor.putInt("count", 23);
		editor.putFloat("PI", 3.1415f);
		editor.commit();
		
	}
	
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值