首页 > 其他分享 >打卡20

打卡20

时间:2024-06-19 17:23:57浏览次数:29  
标签:checkbox 20 remember editText import 打卡 password id

所花时间(包括上课):

 2h

代码量(行):

 150左右

搏客量(篇):

 1

了解到的知识点:

安卓

备注(其他):

 
package com.example.app_02;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app_02.entity.Record;
import com.example.app_02.entity.User;
import com.example.app_02.utils.RecordDao;
import com.example.app_02.utils.UserDao;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

private Vibrator vibrator;
private final long VIBRATION_DURATION = 100; // 震动持续时间100毫秒

private Handler handler;
private UserDao userDao;

private boolean isRememberUserName = false;//是否记住用户名
private boolean isRememberpassword = false;//是否记住密码
private SharedPreferences sharedPreferences;//声明一个共享参数对象


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


TextView textview_login = findViewById(R.id.textview_login);//从布局文件中获取textview_login文本视图
Button button_register_no = findViewById(R.id.button_register_no);//获取跳转到注册页面功能的button按钮的实例
Button button_login = findViewById(R.id.button_login);//获取button登录按钮实例
CheckBox checkbox_remember_id = findViewById(R.id.checkbox_remember_id);//获取CheckBox实例
CheckBox checkbox_remember_password = findViewById(R.id.checkbox_remember_password);//获取CheckBox实例
EditText editText_username = findViewById(R.id.editText_username);//获取EditText实例
EditText editText_password = findViewById(R.id.editText_password);//获取EditText实例


//为每个button按钮控件注册点击监听器
button_register_no.setOnClickListener(this);
button_login.setOnClickListener(this);
//为每个CheckBox控件注册点击监听器
checkbox_remember_id.setOnClickListener(this);
checkbox_remember_password.setOnClickListener(this);

//给checkbox_remember_id设置勾选监听器
checkbox_remember_id.setOnCheckedChangeListener(((buttonView, isChecked) -> isRememberUserName = isChecked));
//给checkbox_remember_password设置勾选监听器
checkbox_remember_password.setOnCheckedChangeListener(((buttonView, isChecked) -> isRememberpassword = isChecked));

sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);//从login.xml获取共享参数实例
String username = sharedPreferences.getString("username", "");//获取共享参数保存的用户名
String password = sharedPreferences.getString("password", "");//获取共享参数保存的密码
boolean ischeckName = sharedPreferences.getBoolean("ischeckName", false);
boolean ischeckPassword = sharedPreferences.getBoolean("ischeckPassword", false);
if (ischeckName) {
editText_username.setText(username);
checkbox_remember_id.setChecked(ischeckName);
}//在用户名编辑框中填写上次保存的用户名
if (ischeckPassword) {
editText_password.setText(password);
checkbox_remember_password.setChecked(ischeckPassword);
}//在密码编辑框中填写上次保存的密码


handler = new Handler(getMainLooper());//获取主线程
userDao = new UserDao();


//设计动态渐变
int gradient_startColor = Color.rgb(148, 0, 211);//定义深紫色为渐变起点
int gradient_endColor = Color.rgb(255, 0, 0);//定义深红色为渐变终点
int[] color_start_end = {gradient_startColor, gradient_endColor};
float[] position = {0f, 1f};
//创建一个LinearGradient渐变对象应用于TextView的Paint对象,实现TextView中文字的渐变效果
LinearGradient shader = new LinearGradient(0, 0, textview_login.getTextSize() * textview_login.getText().length(), textview_login.getTextSize(), color_start_end, position, Shader.TileMode.CLAMP);
textview_login.getPaint().setShader(shader);
//实例化对象,创建透明动画效果,从0.7f到1.0f渐变 0.0是完全透明,1.0完全不透明
AlphaAnimation animation = new AlphaAnimation(0.7f, 1.0f);
//设置动画持续时常 单位:毫秒
animation.setDuration(300);
//设置重复次数
animation.setRepeatCount(Animation.INFINITE);
//设置重复模式
animation.setRepeatMode(Animation.REVERSE);
//给TextView文本添加动画效果
textview_login.startAnimation(animation);


//保持竖屏
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
double angle = Math.atan2(y, x) * 180 / Math.PI;
if (angle < -45 && angle > -135) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else if (angle > 45 && angle < 135) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);


//点击button按钮缩小,松开恢复和点击震动
button_register_no.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//缩小按钮
ScaleAnimation shrinkAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
shrinkAnimation.setDuration(100);
shrinkAnimation.setFillAfter(true);
v.startAnimation(shrinkAnimation);
break;
case MotionEvent.ACTION_UP:
//恢复按钮
ScaleAnimation restoreAnimation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
restoreAnimation.setDuration(100);
restoreAnimation.setFillAfter(true);
v.startAnimation(restoreAnimation);
break;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 按下时开始震动
vibrator.vibrate(VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
break;
case MotionEvent.ACTION_UP:
// 松开时停止震动
vibrator.cancel();
break;
}
return false;
}
});
//点击button按钮缩小,松开恢复和点击震动
button_login.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//缩小按钮
ScaleAnimation shrinkAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
shrinkAnimation.setDuration(100);
shrinkAnimation.setFillAfter(true);
v.startAnimation(shrinkAnimation);
break;
case MotionEvent.ACTION_UP:
//恢复按钮
ScaleAnimation restoreAnimation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
restoreAnimation.setDuration(100);
restoreAnimation.setFillAfter(true);
v.startAnimation(restoreAnimation);
break;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 按下时开始震动
vibrator.vibrate(VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
break;
case MotionEvent.ACTION_UP:
// 松开时停止震动
vibrator.cancel();
break;
}
return false;
}
});


//点击CheckBox震动
checkbox_remember_id.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 按下时开始震动
vibrator.vibrate(VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
break;
case MotionEvent.ACTION_UP:
// 松开时停止震动
vibrator.cancel();
break;
}
return false;
}
});


//点击CheckBox震动
checkbox_remember_password.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 按下时开始震动
vibrator.vibrate(VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
break;
case MotionEvent.ACTION_UP:
// 松开时停止震动
vibrator.cancel();
break;
}
return false;
}
});


}

protected void onResume() {

super.onResume();

CheckBox checkbox_remember_id = findViewById(R.id.checkbox_remember_id);//获取CheckBox实例
CheckBox checkbox_remember_password = findViewById(R.id.checkbox_remember_password);//获取CheckBox实例
EditText editText_username = findViewById(R.id.editText_username);//获取EditText实例
EditText editText_password = findViewById(R.id.editText_password);//获取EditText实例

sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);//从login.xml获取共享参数实例
String username = sharedPreferences.getString("username", "");//获取共享参数保存的用户名
String password = sharedPreferences.getString("password", "");//获取共享参数保存的密码
boolean ischeckName = sharedPreferences.getBoolean("ischeckName", false);
boolean ischeckPassword = sharedPreferences.getBoolean("ischeckPassword", false);
if (ischeckName)
editText_username.setText(username);//在用户名编辑框中填写上次保存的用户名
if (ischeckPassword)
editText_password.setText(password);//在密码编辑框中填写上次保存的密码
// checkbox_remember_id.setChecked(ischeckName);
// checkbox_remember_password.setChecked(ischeckPassword);
}

public void login() {
EditText editText_username = findViewById(R.id.editText_username);//获取EditText实例
EditText editText_password = findViewById(R.id.editText_password);//获取EditText实例
final String username = editText_username.getText().toString().trim();//获取用户输入的用户名
final String password = editText_password.getText().toString().trim();//获取用户输入的密码
if (TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
//弹出提醒对话框,提醒用户用户名不能为空
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("尊敬的用户");
builder.setMessage("用户名不能为空,请输入!");
builder.setPositiveButton("好的", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
layoutParams.width = 700;
layoutParams.height = 565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
editText_username.requestFocus();
} else if (TextUtils.isEmpty(password) && !TextUtils.isEmpty(username)) {
//弹出提醒对话框,提醒用户密码不能为空
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("尊敬的用户");
builder.setMessage("密码不能为空,请输入!");
builder.setPositiveButton("好的", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
layoutParams.width = 700;
layoutParams.height = 565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
editText_password.requestFocus();
} else if (TextUtils.isEmpty(username) && TextUtils.isEmpty(password)) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("尊敬的用户");
builder.setMessage("请输入用户名和密码!");
builder.setPositiveButton("好的", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
layoutParams.width = 700;
layoutParams.height = 565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
editText_username.requestFocus();
editText_password.requestFocus();
} else {
//这里要以线程访问,否则会报错
new Thread(new Runnable() {
@Override
public void run() {
final User user_name = userDao.findUserName(username);
final User user = userDao.findUser(username, password);
//这里使用Handler类中常用的一个方法,post(Runnable r),立即发送Runnable对象。这里使用已经创建的android.os.Handler对象
handler.post(new Runnable() {
@Override
public void run() {
if (user_name == null) {
//创建提醒对话框的建造器
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle("尊敬的用户");
//设置对话框内容文本
builder.setMessage("您所输入的账号不存在,请重新输入!");
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText_username.setText("");//清空editText_username内容
editText_password.setText("");//清空editText_password内容
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("ischeckName", false);
editor.putString("username", "");
editor.putBoolean("ischeckPassword", false);
editor.putString("password", "");
editor.apply();
CheckBox checkbox_remember_id = findViewById(R.id.checkbox_remember_id);//获取CheckBox实例
CheckBox checkbox_remember_password = findViewById(R.id.checkbox_remember_password);//获取CheckBox实例
checkbox_remember_id.setChecked(false);
checkbox_remember_password.setChecked(false);
}
});
AlertDialog alertDialog = builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
layoutParams.width = 700;
layoutParams.height = 565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
}
if (user == null) {
//创建提醒对话框的建造器
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle("尊敬的用户");
//设置对话框内容文本
builder.setMessage("您所输入的密码错误,请重新输入!");
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText_password.setText("");//清空editText_password内容
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("ischeckName", false);
editor.putString("username", "");
editor.putBoolean("ischeckPassword", false);
editor.putString("password", "");
editor.apply();
CheckBox checkbox_remember_id = findViewById(R.id.checkbox_remember_id);//获取CheckBox实例
CheckBox checkbox_remember_password = findViewById(R.id.checkbox_remember_password);//获取CheckBox实例
checkbox_remember_id.setChecked(false);
checkbox_remember_password.setChecked(false);
}
});
AlertDialog alertDialog = builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
layoutParams.width = 700;
layoutParams.height = 565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
} else {
//如果勾选了"记住账号"复选框,就把账号保存到共享参数里
if (isRememberUserName) {
SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器对象
editor.putBoolean("ischeckName", true);
editor.putString("username", editText_username.getText().toString());//添加名为username的账号
editor.apply();//提交编辑器修改
} else {
SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器对象
editor.putBoolean("ischeckName", false);
editText_username.setText("");//清空账号输入框
editor.apply();//提交编辑器修改
}
//如果勾选了“记住密码"复选框,就把密码保存到共享参数里
if (isRememberpassword) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("ischeckPassword", true);
editor.putString("password", editText_password.getText().toString());//添加名为password的密码
editor.apply();
} else {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("ischeckPassword", false);
editText_password.setText("");//清空密码输入框
editor.apply();
}

new Thread(new Runnable() {
@Override
public void run() {
String record = "";
ArrayList<Record> records = new ArrayList<>();
RecordDao recordDao = new RecordDao();
records = recordDao.findRecord();
for (int i = 0; i < records.size(); i++) {
record += records.get(i).toString();
}
sharedPreferences = getSharedPreferences("record", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器对象
editor.putString("record", record);
editor.apply();
}
}).start();

//创建一个意图对象,准备跳转到指定的活动页面
Intent intent = new Intent(LoginActivity.this, FirstPageActivity.class);
//跳转到意图对象指定的活动页面
startActivity(intent);
}

}
});

}
}).start();
}
}


//设计读取button按钮点击的功能函数onClick()
@Override
public void onClick(View v) {
if (v.getId() == R.id.button_register_no) {
//创建一个意图对象,准备跳转到指定的活动页面
Intent intent = new Intent(this, RegisterActivity.class);
//跳转到意图对象指定的活动页面
startActivity(intent);
}
if (v.getId() == R.id.button_login) {
login();
}
}
}

标签:checkbox,20,remember,editText,import,打卡,password,id
From: https://www.cnblogs.com/pinganxile/p/18256703

相关文章

  • 2024/4/21
     所学时间:2小时代码行数:127行博客园数:1篇所学知识:编写web作业,完成了大致页面。<%@pageimport="java.util.*"%><%@pageimport="java.text.*"%><%@pagesession="true"%><%@pagelanguage="java"%><%@pagecon......
  • 打卡19
    所花时间(包括上课): 2h代码量(行): 150左右搏客量(篇): 1了解到的知识点:安卓备注(其他): packagecom.example.app_02;importandroid.annotation.SuppressLint;importandroid.app.DatePickerDialog;importandroid.content.Context;import......
  • 2024/6/14
    学习时长:3小时代码行数:未统计博客数量:1篇今天完成计网实验三的部分,也是最后一次实验S1>enableS1#configConfiguringfromterminal,memory,ornetwork[terminal]?Enterconfigurationcommands,oneperline.EndwithCNTL/Z.S1(config)#vlan10S1(config-vlan)#interfac......
  • 2024/6/2
    今日完成的主要内容是有关于数据库的实验四的内容相关内容如下:数据库的备份与恢复实验在用Windows身份验证进入SSMS后找到服务器对象,右键点击备份设备点击新建备份设备来新建一个备份设备 然后再右键点击新建的备份设备,点击备份数据库 在数据库中找到students数据库 在......
  • 打卡11
    所花时间(包括上课): 2h代码量(行): 100左右搏客量(篇): 1了解到的知识点: mybits备注(其他):  packagecom.leap.jixianceshiboot.service.impl;importcom.github.pagehelper.Page;importcom.github.pagehelper.PageHelper;importcom.le......
  • 打卡12
    所花时间(包括上课): 2h代码量(行): 150左右搏客量(篇): 1了解到的知识点:springboot备注(其他): packagecom.leap.jixianceshiboot.controller;importcom.leap.jixianceshiboot.entity.Policy;importcom.leap.jixianceshiboot.entity.Poli......
  • 打卡13
    所花时间(包括上课): 2h代码量(行): 100左右搏客量(篇): 1了解到的知识点:vue备注(其他): <scriptsetup>import{ref}from'vue'import{getPolicyService}from"@/api/getPolicy.js";import{ElMessage}from"element-plus&quo......
  • P1064 [NOIP2006 提高组] 金明的预算方案
    金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过 ......
  • 离线免费最新超长AI视频模型!一句话即可生成120秒视频,免费开源!只需要一张照片和音频,即
    离线免费最新超长AI视频模型!一句话即可生成120秒视频,免费开源!只需要一张照片和音频,即可生成会说话唱歌的AI视频!能自行完成整个软件项目的AI工具,以及Llama3在线体验和本地安装部署。StreamingT2V(StreamingText-to-Video)模型是一种将文本描述转换为视频内容的人工智能技......
  • YC303C [ 20240617 CQYC省选模拟赛 T3 ] Generals(generals)
    题意给定一张\(n\timesm\)的地图。对于第\(0\)列,第\(m+1\)列,第\(0\)行,第\(n+1\)行,有\(2n+2m\)个人,每个人面朝地图中心。每个人走到别人染过色的位置,或走出地图,将走过的地方染色。你需要求出共有多少种本质不同的染色方案。\(n,m\le10^6\)Sol直接......