<?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:layout_width="match_parent" android:layout_height="match_parent" android:background="#E6E6E6" tools:context=".MainActivity"> <ImageView android:id="@+id/iv_head" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:src="@drawable/dddss" /> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_head" android:layout_margin="10dp" android:background="#ffffff" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="账号" /> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_name" android:background="@null" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#E6E6E6" /> <RelativeLayout android:id="@+id/rl_userpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="密码" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_psw" android:inputType="textPassword" android:background="@null" /> </RelativeLayout> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout" android:layout_centerHorizontal="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:background="#3C8DC4" android:text="登录" android:textColor="#ffffff" /> </RelativeLayout>
package cn.itcase.qq; import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; public class SPSaveQQ { public static boolean saveUserInfor(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; } }
package cn.itcase.qq; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_account; private EditText et_password; private Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Map<String,String>userInfo=SPSaveQQ.getUserInfo(this); if (userInfo!=null){ et_account.setText(userInfo.get("account")); et_password.setText(userInfo.get("password")); } } private void initView(){ et_account=(EditText)findViewById(R.id.et_number); et_password=(EditText)findViewById(R.id.et_password); btn_login=(Button)findViewById(R.id.btn_login); btn_login.setOnClickListener(this); } public void onClick(View view){ switch (view.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_LONG).show(); return; } if (TextUtils.isEmpty(password)){ Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show(); return; } Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show(); boolean isSaveSuccess=SPSaveQQ.saveUserInfor(this,account,password); if (isSaveSuccess){ Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show(); } else { Toast.makeText(this,"保存失败",Toast.LENGTH_LONG).show(); } break; } } }