sharedpreference存储用户名和密码

<span style="font-size:18px;">package com.melody.sharedpreference;

import java.io.FileInputStream;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.content.SharedPreferencesCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText et_name;
	private EditText et_passwd;
	private CheckBox cb;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		init();
		read();
		
	}
	
	//读数据用户名和密码
	private void read() {
		SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
		/*
		 * arg0: key
		 * arg1: 表示如果key取不到,回显什么,默认 回显空字符串
		 */
		String name = sp.getString("name", "");
		String passwd = sp.getString("passwd", "");
		//回显
		et_name.setText(name);
		et_passwd.setText(passwd);
	}
	
	
	//初始化控件
	private void init() {
		et_name = (EditText) findViewById(R.id.et_name);
		et_passwd = (EditText) findViewById(R.id.et_passwd);
		cb = (CheckBox) findViewById(R.id.cb);
	}
	
	//点击登录按钮,保存用户名和密码
	public void login(View v){
		//获取用户输入的用户名和密码
		String name = et_name.getText().toString();
		String passwd = et_passwd.getText().toString();
		
		//判断是否选中
		if(cb.isChecked()){ 
			//获取sharedpreferences
			SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
			//获取编辑器
			Editor et = sp.edit();
			et.putString("name", name);
			et.putString("passwd", passwd);
			//提交
			et.commit();
		}
		//创建吐司对话框
		Toast.makeText(this, "登录成功", 0).show();
		
	}
	
}
</span>
<span style="font-size:18px;">/布局</span>
<span style="font-size:18px;"><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">


    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入帐号" />
    
    <EditText
        android:id="@+id/et_passwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      <span style="white-space:pre">	</span>android:hint="请输入密码" />
   
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<span style="white-space:pre">	</span>    <CheckBox 
<span style="white-space:pre">	</span>        android:id="@+id/cb"
<span style="white-space:pre">	</span>        android:layout_width="wrap_content"
<span style="white-space:pre">	</span>        android:layout_height="wrap_content"
<span style="white-space:pre">	</span>        android:text="记住帐号和密码"
<span style="white-space:pre">	</span>        android:layout_centerVertical="true"
<span style="white-space:pre">	</span>        />
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>    <Button 
<span style="white-space:pre">	</span>        android:layout_width="wrap_content"
<span style="white-space:pre">	</span>        android:layout_height="wrap_content"
<span style="white-space:pre">	</span>        android:layout_alignParentRight="true"
<span style="white-space:pre">	</span>        android:onClick="login"
<span style="white-space:pre">	</span>        android:text="登录"/>
    </RelativeLayout>
</LinearLayout>
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值