• 代码记录


    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;

    }
    }
    }
  • 相关阅读:
    leetcode 237: Delete Node in a Linked List
    谈谈地图中的道路绘制
    关于double类型数字相加位数发生变化的问题
    iOS_25_彩票设置的cell的数据源模型的封装
    session_start() [function.session-start]:
    Android系统Gps分析(一)【转】
    Android4.4 GPS框架分析【转】
    GPIO设备虚拟文件结点的创建【转】
    高通MSM8255 GPS 调试分析&&Android系统之Broadcom GPS 移植【转】
    和菜鸟一起学android4.0.3源码之硬件gps简单移植【转】
  • 原文地址:https://www.cnblogs.com/pomodoro/p/11366046.html
Copyright © 2020-2023  润新知