• 第八次作业


    所需截图:

     

    所需代码:

    <?xml version="1.0" encoding="utf-8"?>
    <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:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="50dp"
    android:src="@drawable/qq" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="30dp">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="账号:"
    android:textSize="30sp" />

    <EditText
    android:id="@+id/etName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入账号"
    android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp">

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

    <EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码"
    android:inputType="textPassword"
    android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="30dp">

    <CheckBox
    android:id="@+id/cbRemember"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="记住密码"
    android:textSize="20sp" />

    <Button
    android:id="@+id/btnLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:onClick="btnLogin"
    android:text="登录"
    android:textSize="20sp" />

    <Button
    android:id="@+id/btnExit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="取消"
    android:textSize="20sp" />

    </LinearLayout>


    </LinearLayout>

    package com.example.myapplication;

    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;

    import androidx.appcompat.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    private EditText etName;
    private EditText etPassword;
    private CheckBox cbRemember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etName = findViewById(R.id.etName);
    etPassword = findViewById(R.id.etPassword);
    cbRemember = findViewById(R.id.cbRemember);
    SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
    etName.setText(sp.getString("name", ""));
    etPassword.setText(sp.getString("password", ""));

    }

    public void btnLogin(View view) {
    if ("admin".equals(etName.getText().toString()) && "admin".equals(etPassword.getText().toString())) {
    if (cbRemember.isChecked()) {
    SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("name", etName.getText().toString());
    editor.putString("password", etPassword.getText().toString());
    editor.commit();
    Toast.makeText(this, "登陆成功,已保存", Toast.LENGTH_SHORT).show();
    } else {
    SharedPreferences sp = getSharedPreferences("date", MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("name", "");
    editor.putString("password", "");
    editor.commit();
    Toast.makeText(this, "登陆成功,未保存", Toast.LENGTH_SHORT).show();
    }
    } else {
    Toast.makeText(this, "密码错误,请重试", Toast.LENGTH_SHORT).show();
    etName.setText("");
    etPassword.setText("");
    }
    }
    }
  • 相关阅读:
    ScrollView嵌套EditText联带滑动的解决的方法
    POJ 2003 Hire and Fire (多重链表 树结构 好题)
    leetcode笔记:Bulls and Cows
    PHP中使用ActiveMQ实现消息队列
    WPF模拟键盘输入和删除
    DLLImport的用法C#
    Net Core 的配置模式以及热重载配置
    简体与繁体转换
    Webdings字体、Wingdings字体对照表、用CSS3绘制的各种小图标
    查询大于2分钟的数据
  • 原文地址:https://www.cnblogs.com/369236941jp/p/11801423.html
Copyright © 2020-2023  润新知