Android 使用SharedPreferences实现记住密码,登录成功后使用Intent跳转并传参

1.编写布局界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

​    <TextView
​        android:layout_width="90dp"
​        android:layout_height="wrap_content"
​        android:layout_gravity="center_vertical"
​        android:text="账号:"
​        android:textSize="18sp" />

​    <EditText
​        android:id="@+id/account"
​        android:layout_width="0dp"
​        android:layout_height="wrap_content"
​        android:layout_gravity="center_vertical"
​        android:layout_weight="1" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

​    <TextView
​        android:layout_width="90dp"
​        android:layout_height="wrap_content"
​        android:layout_gravity="center_vertical"
​        android:text="密码:"
​        android:textSize="18sp" />

​    <EditText
​        android:id="@+id/password"
​        android:layout_width="0dp"
​        android:layout_height="wrap_content"
​        android:layout_gravity="center_vertical"
​        android:layout_weight="1"
​        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

​    <CheckBox
​        android:id="@+id/remember_pwd"
​        android:layout_width="wrap_content"
​        android:layout_height="wrap_content" />

​    <TextView
​        android:layout_width="wrap_content"
​        android:layout_height="wrap_content"
​        android:text="记住密码"
​        android:textSize="18sp" />

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

​    <Button
​        android:id="@+id/login"
​        android:layout_width="0dp"
​        android:layout_height="match_parent"
​        android:layout_weight="1"
​        android:text="登录" />

​    <Button
​        android:id="@+id/reset"
​        android:layout_width="0dp"
​        android:layout_height="match_parent"
​        android:layout_weight="1"
​        android:text="重置"
​        android:onClick="doReset"/>

</LinearLayout>

</LinearLayout>

2.MainActivity代码如下

private Button btnLogin;//登录按钮
private Button btnReset;//重置按钮
private EditText userName;//用户名
private EditText userPassword;//密码
private CheckBox checkBox;//复选框
private SharedPreferences sharedPreferences;//接口:读取对象
private SharedPreferences.Editor editor;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnLogin = (Button) findViewById(R.id.login);
    btnReset = (Button) findViewById(R.id.reset);
    userName = (EditText) findViewById(R.id.account);
    userPassword = (EditText) findViewById(R.id.password);
    checkBox = (CheckBox) findViewById(R.id.remember_pwd);

​    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
​    boolean isRemember = sharedPreferences.getBoolean("remember_password", false);
​    if (isRemember) {
​        userName.setText(sharedPreferences.getString("Name", ""));
​        userPassword.setText(sharedPreferences.getString("Password", ""));
​        checkBox.setChecked(true);
​    }

​    btnLogin.setOnClickListener(new View.OnClickListener() {
​        @Override
​        public void onClick(View v) {
​            editor = sharedPreferences.edit();
​            String name = userName.getText().toString();
​            String password = userPassword.getText().toString();
​            if (name.equals("admin") && password.equals("123456")) {
​                if (checkBox.isChecked()) {
​                    editor.putString("Name", name);
​                    editor.putString("Password", password);
​                    editor.putBoolean("remember_password", true);
​                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
​                    intent.putExtra("name", name);//传递参数
​                    startActivity(intent);
​                } else {
​                    editor.clear();//清空SharedPreferences中的数据
​                }
​                editor.apply();
​            }
​        }
​    });
}
​    public void doReset(){
​        userName.setText("");
​        userPassword.setText("");
​    }

3.编写登录成功后的界面

<?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_main2"
    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="barrage.itheima.com.loginactivity.Main2Activity">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

4.编写登录成功跳转后的Activity

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent intent = getIntent();//得到Intent
    textView = (TextView) findViewById(R.id.textView);
    textView.setText("欢迎登录"+ intent.getStringExtra("name"));
}

猜你喜欢

转载自blog.csdn.net/qq_43249816/article/details/88787268