public class QQ { public static boolean saveUserIfo(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("password",password ); return userMap; } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_account=(EditText)findViewById(R.id.et_account); et_password=(EditText)findViewById(R.id.et_password); btn_login =(Button)findViewById(R.id.btn_login); Map<String, String>userInfo=FileSaveQQ.getUserInfo(this); if(userInfo !=null) { et_account.setText(userInfo.get("account")); et_password.setText(userInfo.get("password")); } } public void Click(View v) { switch(v.getId()) { case R.id.btn_login: String account = et_account.getText().toString().trim(); String password = et_password.getText().toString(); if(TextUtils.isEmpty(account)) { Toast.makeText(this, "请输入QQ账号", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show(); boolean isSaveSuccess = FileSaveQQ.saveUserIfo(this, account, password); if(isSaveSuccess) { Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } break; } } }
<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: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="cn.itcast.saveqq.MainActivity" android:background="#e6e6e6" android:orientation="vertical" android:padding="10dp" > <ImageView android:layout_width="70dp" android:layout_height="70dp" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:src="@drawable/ic_luncher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="账号" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:padding="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="密码" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:inputType="textPassword" android:padding="10dp"/> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:background="#3c8dc4" android:text="登录" android:textColor="@android:color/white" android:textSize="20sp" android:onClick="Click" /> </LinearLayout>
![](https://img2018.cnblogs.com/i-beta/1785088/201912/1785088-20191206161043459-583654078.png)