package com.example.an2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class SPSaveQQ { /** * 保存数据 * * @param context * @param account * @param password * @return */ public static boolean saveUserInfo(Context context, String account, String password) { SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("userName", account); edit.putString("pwd", password); edit.commit(); return true; } public static Map<String, String> getUserInfo(Context context) { SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE); String account = sp.getString("userName", null); String password = sp.getString("pwd", null); Map<String,String> userMap = new HashMap<String, String>(); userMap.put("account", account); userMap.put("passeord", password); return userMap; } }
package com.example.an2; import java.util.Map; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private EditText et1; private EditText et2; private Button bu1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1 = (EditText) findViewById(R.id.et1); et2 = (EditText) findViewById(R.id.et2); bu1 = (Button) findViewById(R.id.bu1); Map<String, String> userInfo = SPSaveQQ.getUserInfo(this); if (userInfo != null) { et1.setText(userInfo.get("account")); et2.setText(userInfo.get("passeord")); } } public void Click(View view) { // Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT); switch (view.getId()) { case R.id.bu1: String account = et1.getText().toString().trim(); String password = et2.getText().toString(); if (TextUtils.isEmpty(account)) { Toast.makeText(MainActivity.this, "请输入QQ账号", Toast.LENGTH_SHORT) .show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); boolean isSaveSuccee = SPSaveQQ.saveUserInfo(MainActivity.this, account, password); if (isSaveSuccee) { Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); } break; default: break; } } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.homework2.MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="90dp" android:text="账号" android:textSize="30sp" android:background="#FFFFFF" /> <EditText android:id="@+id/et1" android:layout_width="200sp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv1" android:layout_alignParentRight="true" android:layout_alignTop="@+id/tv1" android:layout_toRightOf="@+id/tv1" android:phoneNumber="true" android:background="#FFFFFF" android:textSize="20sp" > </EditText> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="140sp" android:text="密码" android:textSize="30sp" android:background="#FFFFFF" /> <EditText android:id="@+id/et2" android:layout_width="200sp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv2" android:layout_alignParentRight="true" android:layout_alignTop="@+id/tv2" android:layout_toRightOf="@+id/tv2" android:password="true" android:background="#FFFFFF" android:textSize="20sp" > </EditText> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/bt1" android:layout_centerHorizontal="true" android:layout_marginTop="98dp" android:textSize="30sp" /> <ImageView android:layout_width="45sp" android:layout_height="45sp" android:layout_above="@+id/et1" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" android:layout_marginBottom="18dp" /> <Button android:id="@+id/bu1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv2" android:layout_below="@+id/tv2" android:layout_marginTop="69dp" android:background="#63B8FF" android:onClick="Click" android:text="登录" android:textSize="30sp" /> </RelativeLayout>