1、学习进度表:
2、任务:
完成第一阶段
3、预计时间:
2天
4、今日完成情况:
1、汇总情况:
- 登录页面MainActivity 设置了记住密码功能
package com.example.reporttemperature; import androidx.appcompat.app.AppCompatActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.content.SharedPreferences; import android.text.method.PasswordTransformationMethod; import android.view.*; import android.widget.Button; import android.content.*; import android.widget.CheckBox; import android.widget.*; public class MainActivity extends AppCompatActivity { private EditText ed_id,ed_password,e_id,e_pwd,et_id,et_password; private Button button_login,button_register; private CheckBox button_rem; private MyOpenHelper helper=new MyOpenHelper(this); private SharedPreferences mSpSettings=null;//声明一个sharedPreferences用于保存数据 private static final String PREPS_NAME="NamePwd"; private boolean flag,flag2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); setTitle("登录界面"); findById();//获取控件 setListener();//绑定事件 getData(); //隐藏密码 ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()); } //绑定事件 private void setListener(){ //注册 button_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到注册页面 Intent intent = new Intent(MainActivity.this, RegisterActivity.class); startActivity(intent); finish(); } }); //登录 button_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str1=ed_id.getText().toString(); String str2=ed_password.getText().toString(); flag=compare(str1,str2); if(flag){ //密码账号比对成功 button_rem=findViewById(R.id.checkBox_auto); //判断复选框是否选中——记住密码 if(button_rem.isChecked()){ mSpSettings=getSharedPreferences(PREPS_NAME,MODE_PRIVATE); SharedPreferences.Editor edit=mSpSettings.edit();//得到Editor对象 edit.putBoolean("isKeep",true);//记录保存标记 edit.putString("username",ed_id.getText().toString());//记录用户ID edit.putString("password",ed_password.getText().toString());//记录用户密码 edit.commit();//提交 }else{ mSpSettings=getSharedPreferences(PREPS_NAME,MODE_PRIVATE); SharedPreferences.Editor edit=mSpSettings.edit() ; edit.putBoolean("isKeep",true);//记录保存标记 edit.putString("username",""); edit.putString("password",""); edit.commit();//提交 } //登录成功跳转页面 Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show(); Intent intent = new Intent(MainActivity.this, CatalogActivity.class); intent.putExtra("IDnumber",ed_id.getText().toString()); //finish(); startActivity(intent); }else{ //登陆失败 Toast.makeText(MainActivity.this, "密码或者用户名错误", Toast.LENGTH_SHORT).show(); } } }); } //登录时比对信息 public boolean compare(String id,String password){ flag2=false; SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.rawQuery("select * from user",null); while(cursor.moveToNext()){ String id_number=cursor.getString(cursor.getColumnIndex("idNumber")); String password_user=cursor.getString(cursor.getColumnIndex("password")); if(id.equals(id_number)&&password.equals(password_user)) { flag2 = true; break; } } return flag2; } @Override protected void onResume() { super.onResume(); getData();//在界面显示数据之前得到之前存储的数据 } private void getData() { mSpSettings=getSharedPreferences(PREPS_NAME, MODE_PRIVATE); if(mSpSettings.getBoolean("isKeep", false)){ //如果之前存储过,则显示在相应文本框内 ed_id.setText(mSpSettings.getString("username", "")); ed_password .setText(mSpSettings.getString("password", "")); }else{//否则显示为空 ed_id.setText(""); ed_password.setText(""); } } private void findById(){ ed_id=findViewById(R.id.input_ID); ed_password=findViewById(R.id.input_password); button_login=findViewById(R.id.button_login); button_register=findViewById(R.id.button_register); } }
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/main_activity" tools:context=".MainActivity"> <Button android:id="@+id/button_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginStart="94dp" android:layout_marginLeft="94dp" android:layout_marginTop="40dp" android:layout_marginEnd="57dp" android:layout_marginRight="57dp" android:layout_marginBottom="165dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:text="登录" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/button_register" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox_auto" /> <Button android:id="@+id/button_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="183dp" android:layout_marginEnd="84dp" android:layout_marginRight="84dp" android:layout_marginBottom="129dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:text="注册" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/input_password"> </Button> <TextView android:id="@+id/textView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="25dp" android:layout_marginLeft="25dp" android:layout_marginTop="136dp" android:layout_marginEnd="29dp" android:layout_marginRight="29dp" android:text="账号" android:textSize="24dp" app:layout_constraintEnd_toStartOf="@+id/input_ID" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/input_ID" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="179dp" android:layout_marginEnd="59dp" android:layout_marginRight="59dp" android:layout_marginBottom="69dp" android:hint="请输入学号" android:maxLines="1" android:textSize="28dp" app:layout_constraintBottom_toTopOf="@+id/input_password" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView_ID" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="25dp" android:layout_marginLeft="25dp" android:layout_marginEnd="29dp" android:layout_marginRight="29dp" android:layout_marginBottom="8dp" android:text="密码" android:textSize="24dp" app:layout_constraintBottom_toBottomOf="@+id/input_password" app:layout_constraintEnd_toStartOf="@+id/input_password" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/input_password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="69dp" android:layout_marginEnd="58dp" android:layout_marginRight="58dp" android:hint="请输入密码" android:maxLines="1" android:textSize="28dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView_password" app:layout_constraintTop_toBottomOf="@+id/input_ID" /> <CheckBox android:id="@+id/checkBox_auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="34dp" android:layout_marginEnd="181dp" android:layout_marginRight="181dp" android:layout_marginBottom="117dp" android:text="记住密码" android:textSize="24dp" app:layout_constraintBottom_toTopOf="@+id/button_login" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/input_password" app:layout_constraintTop_toBottomOf="@+id/input_password" /> </androidx.constraintlayout.widget.ConstraintLayout>