• 代码记录


    activity_login.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
    android:id="@+id/layout1"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:textSize="18sp"
    android:text="账号:" />

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

    <LinearLayout
    android:id="@+id/layout2"
    android:layout_below="@id/layout1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:textSize="18sp"
    android:text="密码:" />

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

    <LinearLayout
    android:id="@+id/layout3"
    android:layout_below="@id/layout2"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
    android:id="@+id/remember_pass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

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

    </LinearLayout>

    <Button
    android:id="@+id/login"
    android:layout_below="@id/layout3"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#FF4081"
    android:text="登录" />

    <Button
    android:id="@+id/register"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:textSize="16sp"
    android:textColor="#928E8E"
    android:text="注册" />

    </RelativeLayout>


    loginActivity.java:
    package com.example.bizarredictionary;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;


    public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private SharedPreferences pref;

    private SharedPreferences.Editor editor;

    private EditText accountEdit;

    private EditText passwordEdit;

    private Button login, register;

    private CheckBox rememberPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    pref = PreferenceManager.getDefaultSharedPreferences(this); //获取SharedPreferences对象,自动使用当前应用程序的包名作为前缀来命名SharedPreferences文件。
    accountEdit = (EditText) findViewById(R.id.account);
    passwordEdit = (EditText) findViewById(R.id.password);
    rememberPass = (CheckBox) findViewById(R.id.remember_pass);

    Button login = findViewById(R.id.login);
    Button regidter = findViewById(R.id.register);
    login.setOnClickListener(this);
    regidter.setOnClickListener(this);

    boolean isRemember = pref.getBoolean("remember_password", false);
    boolean iskeep = pref.getBoolean("keep_login", false);

    if (iskeep) {
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
    }
    if (isRemember) {
    // 将账号和密码都设置到文本框中
    String account = pref.getString("account", "");
    String password = pref.getString("password", "");
    accountEdit.setText(account);
    passwordEdit.setText(password);
    rememberPass.setChecked(true);
    }
    }

    @Override
    public void onClick(View v) {
    String account = accountEdit.getText().toString();
    String password = passwordEdit.getText().toString();
    switch(v.getId()){
    case R.id.login:
    // 如果账号是admin且密码是123456,就认为登录成功
    if (account.equals("admin") && password.equals("123456")) {
    editor = pref.edit();
    if (rememberPass.isChecked()) { // 检查复选框是否被选中
    editor.putBoolean("remember_password", true);
    editor.putString("account", account);
    editor.putString("password", password);
    } else {
    editor.clear();
    }
    editor.putBoolean("keep_login", true);
    editor.apply();

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
    } else {
    Toast.makeText(LoginActivity.this, "account or password is invalid",
    Toast.LENGTH_SHORT).show();
    }

    case R.id.register:
    //if (account.equals(db.))
    break;

    }
    }
    }
  • 相关阅读:
    nginx中目录转义字符问题
    2022年3月2日最近的状态~
    [esp8266]官方SDK与arduino ROM或Flash布局,Vscode+platformio 如何设置
    什么是并发容器
    Executor, ExecutorService 和 Executors区别与联系
    Spirng和SpringBoot中的Aop优先使用的是JDK动态代理还是Cglib
    建造者模式
    MySQL四大排名函数
    SpringMVC执行流程解析(PS:使用了适配器模式)
    适配器模式
  • 原文地址:https://www.cnblogs.com/pomodoro/p/11366046.html
Copyright © 2020-2023  润新知