个人作业阶段一 1
从今天开始要在之前寒假完成的 寒假福利 基础上,实现一个小型体温上报系统的开发。
个人作业阶段一要求:
1)要求增加用户注册功能,用户注册信息包括用户ID(学号)、用户名(姓名),手机号码,用户单位(班级),用户班级四项基本信息,用户第一次注册后,用户姓名不用每次输入。
2)提问上报界面包括用户姓名(自动从注册信息获取)、测力时间(精确到年月日时分即可)、所在位置(自动获取)、测量体温(缺省值为36.2),特殊情况(见下图)五项内容
首先先画一个注册界面
activity_register.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".RegisterActivity" android:background="@drawable/bk4"> <EditText android:id="@+id/user_xuehao" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#000000" android:hint="学号" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_marginTop="100dp" android:layout_marginBottom="10dp" android:layout_marginHorizontal="10dp" android:drawableLeft="@drawable/icon_xuehao" android:drawablePadding="10dp" android:maxLines="1"/> <EditText android:id="@+id/user_name" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#000000" android:hint="姓名" android:layout_below="@+id/user_xuehao" android:layout_marginTop="15dp" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:maxLines="1"/> <EditText android:id="@+id/user_phone" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#000000" android:hint="电话号码" android:layout_below="@+id/user_name" android:layout_marginTop="15dp" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp" android:drawableLeft="@drawable/icon_phone" android:drawablePadding="10dp" android:maxLines="1"/> <EditText android:id="@+id/user_class" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#000000" android:hint="所在班级" android:layout_below="@+id/user_phone" android:layout_marginTop="15dp" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp" android:drawableLeft="@drawable/icon_class" android:drawablePadding="10dp" android:maxLines="1"/> <CheckBox android:id="@+id/rem_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/user_class" android:text="记住信息" android:textSize="18sp" android:textColor="#F0FFFF" android:layout_margin="10dp" /> <CheckBox android:id="@+id/auto_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/user_class" android:text="自动登录" android:textSize="18sp" android:textColor="#F0FFFF" android:layout_marginTop="10dp" android:layout_marginLeft="100dp" android:layout_toRightOf="@+id/rem_info" /> <Button android:id="@+id/user_zhuce" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@+id/auto_login" android:text ="注册" android:textSize="20sp" android:background="@drawable/bg_btn4" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp"/> </RelativeLayout>
登录这里实现了一个记住密码和自动登录功能
利用 了 SharedPreferences 相关技术
register.java
package com.example.myapplication; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; import org.litepal.LitePal; import java.util.List; public class RegisterActivity extends Activity { private SharedPreferences sp; private Button zhuce; //注册按钮 private CheckBox auto_login; //自动登录勾选框 private CheckBox rem_info; //记住信息勾选框 private EditText user_xuehao, user_name, user_phone, user_class; //输入学号、姓名、电话号码、所在班级 public static String userxuehao, username, userphone, userclass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉顶部 setContentView(R.layout.activity_register); sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE); user_xuehao = findViewById(R.id.user_xuehao); //学号 user_name = findViewById(R.id.user_name); //姓名 user_phone = findViewById(R.id.user_phone); //电话号码 user_class = findViewById(R.id.user_class); //所在班级 zhuce = findViewById(R.id.user_zhuce); //注册按钮 auto_login = findViewById(R.id.auto_login); //自动登录 rem_info = findViewById(R.id.rem_info); //记住信息 //判断记住信息勾选框状态 if(sp.getBoolean("rem_isCheck",false)){ rem_info.setChecked(true); user_xuehao.setText(sp.getString("USER_XUEHAO", "")); user_name.setText(sp.getString("USER_NAME", "")); user_phone.setText(sp.getString("USER_PHONE", "")); user_class.setText(sp.getString("USER_CLASS", "")); //判断自动登陆多选框状态 if (sp.getBoolean("auto_isCheck", false)) { //设置默认是自动登录状态 auto_login.setChecked(true); //跳转界面 Intent intent = new Intent(RegisterActivity.this, MenuActivity.class); intent.putExtra("name",user_name.getText().toString()); //把注册时填写的用户名称传值到填写界面 intent.putExtra("xuehao",userxuehao); //传递学号 intent.putExtra("phone",userphone); //传递电话号码 username=user_name.getText().toString(); userxuehao=user_xuehao.getText().toString(); userphone=user_phone.getText().toString(); RegisterActivity.this.startActivity(intent); //finish(); } } //注册按钮 zhuce.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { userxuehao = user_xuehao.getText().toString(); username = user_name.getText().toString(); userphone = user_phone.getText().toString(); userclass = user_class.getText().toString(); //数据库操作 User user = new User(); List<User>users = LitePal.findAll(User.class); for(int i=0;i<users.size();i++){ if(users.get(i).getUser_xuehao()==userxuehao){ users.get(i).delete(); } } user.setUser_xuehao(userxuehao); user.setUser_name(username); user.setUser_phone(userphone); user.setUser_class(userclass); user.save(); //注册信息不完整时 if (TextUtils.isEmpty(userxuehao)) { Toast.makeText(RegisterActivity.this, "学号不能为空", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(username)) { Toast.makeText(RegisterActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(userphone)) { Toast.makeText(RegisterActivity.this, "电话号码不能为空", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(userclass)) { Toast.makeText(RegisterActivity.this, "所在班级不能为空", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(RegisterActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); //登录成功 if (rem_info.isChecked()) { //记住信息 Editor editor = sp.edit(); editor.putString("USER_XUEHAO", userxuehao); editor.putString("USER_NAME", username); editor.putString("USER_PHONE", userphone); editor.putString("USER_CLASS", userclass); editor.commit(); } //跳转界面 Intent intent = new Intent(RegisterActivity.this, MenuActivity.class); intent.putExtra("name",username); intent.putExtra("xuehao",userxuehao); intent.putExtra("phone",userphone); RegisterActivity.this.startActivity(intent); //finish(); } } }); //监听记住密码多选框按钮事件 rem_info.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (rem_info.isChecked()) { System.out.println("记住信息已选中"); sp.edit().putBoolean("rem_isCheck", true).commit(); } else { System.out.println("记住信息没有选中"); sp.edit().putBoolean("rem_isCheck", false).commit(); } } }); //监听自动登录多选框事件 auto_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (auto_login.isChecked()) { System.out.println("自动登录已选中"); sp.edit().putBoolean("auto_isCheck", true).commit(); } else { System.out.println("自动登录没有选中"); sp.edit().putBoolean("auto_isCheck", false).commit(); } } }); } }