1.在activity_main.xml中进行配置
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f6f" android:orientation="vertical" tools:context="com.edu.bzu.sharedpreferences.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout" android:layout_margin="10dp" android:background="#ffffff" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:id="@+id/r1_username" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="账号"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_number" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/tv_name" android:background="#f2f2"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="10dp" android:background="#E6E6E6" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:id="@+id/r1_userpsw"> <TextView android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="密码"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_password" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_psw" android:background="#f2f2"/> </RelativeLayout> </LinearLayout> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:src="@drawable/deer" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/iv_head"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_login" android:layout_below="@id/layout" android:layout_centerHorizontal="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#3C8DC4" android:text="登录" /> </RelativeLayout> 结果如图所示2.在MainActivity中进行编写配置
package com.edu.bzu.sharedpreferences; import android.app.Activity; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.Map; import com.edu.bzu.sharedpreferences.utils.Utils; public class MainActivity extends Activity implements View.OnClickListener { private EditText etNumber; private EditText etPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Map<String,String> userInfo=Utils.getUserInfo(this); if(userInfo!=null){ etNumber.setText(userInfo.get("number")); etPassword.setText(userInfo.get("password")); } } private void initView(){ etNumber=(EditText) findViewById(R.id.et_number); etPassword=(EditText) findViewById(R.id.et_password); findViewById(R.id.btn_login).setOnClickListener(this); } public void onClick(View v){ String number=etNumber.getText().toString().trim(); String password=etPassword.getText().toString(); if (TextUtils.isEmpty(number)){ Toast.makeText(this,"请输入QQ号码",Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)){ Toast.makeText(this,"请输入QQ密码",Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show(); Log.i("MainActivity","记住密码:"+number+","+password); boolean isSaveSucess=Utils.saveUserInfo(this,number,password); if (isSaveSucess){ Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show(); } } } 运行结果如图:关闭程序后,重新开启,依然保存着账号和密码。