目录
一、建用户表(User)
软件开发的使用是需要登录和注册的,用户注册的信息也是保存在本地的数据库文件里面的;所以需要先建立数据库的表文件;具体代码是:
@Table(name = "User")
public class User extends SugarRecord {
private Long id;
//用户名称
@Column(name = "userName")
private String userName;
//用户手机号码
@Column(name = "userPhone")
private String userPhone;
//用户密码
@Column(name = "userPswd")
private String userPswd;
//注册时间
@Column(name = "userTime")
private String userTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getUserPswd() {
return userPswd;
}
public void setUserPswd(String userPswd) {
this.userPswd = userPswd;
}
public String getUserTime() {
return userTime;
}
public void setUserTime(String userTime) {
this.userTime = userTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", userPhone='" + userPhone + '\'' +
", userPswd='" + userPswd + '\'' +
", userTime='" + userTime + '\'' +
'}';
}
}
二、用户注册
1:注册界面的编写
<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:background="@color/bg_color_login"
android:orientation="vertical"
tools:context=".UnLoginActiviy">
<include
android:id="@+id/rl_title"
layout="@layout/title_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="用户姓名:"
android:textColor="#333333"
android:textSize="16dp" />
<EditText
android:id="@+id/metName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入用户姓名"
android:paddingLeft="10dp"
android:textSize="14dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="@color/bg_color_login" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="手机号码:"
android:textColor="#333333"
android:textSize="16dp" />
<EditText
android:id="@+id/metPhone"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入手机号码"
android:inputType="number"
android:maxLength="11"
android:paddingLeft="10dp"
android:textSize="14dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="@color/bg_color_login" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="登录密码:"
android:textColor="#333333"
android:textSize="16dp" />
<EditText
android:id="@+id/metPswd"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入6-11位密码"
android:maxLength="11"
android:paddingLeft="10dp"
android:password="true"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/mbtnReg"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/show_one"
android:layout_margin="10dp"
android:background="@drawable/select_btn"
android:gravity="center_vertical|center_horizontal"
android:text="注册"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2:注册界面数据的获取
public class RegisterActivity extends BaseActivity {
private int choiceType = 1;
// title
private TextView mTvTitle;
// 返回
private ImageView mIvBack;
// 查询按钮
private Button mbtnReg;
private EditText metName;
private EditText metPhone;
private EditText metPswd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initWidget();
initData();
}
@Override
public void initWidget() {
metName = (EditText) findViewById(R.id.metName);
metPhone = (EditText) findViewById(R.id.metPhone);
metPswd = (EditText) findViewById(R.id.metPswd);
mbtnReg = (Button) findViewById(R.id.mbtnReg);
mIvBack = (ImageView) findViewById(R.id.mIvBack);
mTvTitle = (TextView) findViewById(R.id.mTvTitle);
mTvTitle.setText("注册");
mIvBack.setVisibility(View.VISIBLE);
mIvBack.setOnClickListener(this);
mbtnReg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mIvBack:
RegisterActivity.this.finish();
break;
case R.id.mbtnReg:
if (TextUtils.isEmpty(metName.getText().toString())) {
ToastUtil.ShowCentre(RegisterActivity.this, "请输入用户名称");
return;
}
if (TextUtils.isEmpty(metPhone.getText().toString())) {
ToastUtil.ShowCentre(RegisterActivity.this, "请输入手机号码");
return;
}
if (TextUtils.isEmpty(metPswd.getText().toString())) {
ToastUtil.ShowCentre(RegisterActivity.this, "请输入密码");
return;
}
//对用户信息进行注册保存
User userModel=new User();
userModel.setUserName(metName.getText().toString());//用户名称
userModel.setUserPhone(metPhone.getText().toString());//手机号码
userModel.setUserPswd(metPswd.getText().toString());//登录密码
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置日期格式
userModel.setUserTime(df.format(new Date()));
userModel.save();
CustomToast.showToast(this, "注册成功");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 1000);
break;
}
}
@Override
public void initData() {
}
}
3:代码说明
//对用户信息进行注册保存 User userModel=new User(); userModel.setUserName(metName.getText().toString());//用户名称 userModel.setUserPhone(metPhone.getText().toString());//手机号码 userModel.setUserPswd(metPswd.getText().toString());//登录密码 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置日期格式 userModel.setUserTime(df.format(new Date())); userModel.save();
说明:注册界面最重要的就是这块的代码了;这块的代码就是对用户输入的信息进行保存;然后用来用户的登录时候进行使用;
三、用户登录界面
1:登录界面的编写
<RelativeLayout 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:background="@color/main_color"
android:orientation="vertical"
tools:context=".UnLoginActiviy" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/input_ok"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical" >
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/icon_login_phone" />
<EditText
android:id="@+id/mLoginNumber"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入手机号码"
android:paddingLeft="10dp"
android:textSize="14dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="@color/bg_color_login"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical" >
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/icon_login_suo" />
<EditText
android:id="@+id/mLoginPswd"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入登录密码"
android:paddingLeft="10dp"
android:password="true"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/mLogin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/input_ok"
android:gravity="center_vertical|center_horizontal"
android:text="用户登录"
android:textColor="@color/main_color"
android:textSize="15dp" />
<Button
android:id="@+id/mEnterpriseQuery"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/show_one"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/input_ok"
android:gravity="center_vertical|center_horizontal"
android:text="用户注册"
android:textColor="@color/main_color"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
2:登录界面逻辑代码
public class LoginActivity extends BaseActivity {
// title
private TextView mTvTitle;
// 登录用户名称
private EditText mLoginNumber;
// 登录密码
private EditText mLoginPswd;
// 登录按钮
private Button mLogin;
private Button mEnterpriseQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginnew);
initWidget();
}
@Override
public void initWidget() {
mdialog = new LoadingDialog(this, "正在登录");
mTvTitle = (TextView) findViewById(R.id.mTvTitle);
mLoginNumber = (EditText) findViewById(R.id.mLoginNumber);
mLoginPswd = (EditText) findViewById(R.id.mLoginPswd);
mLogin = (Button) findViewById(R.id.mLogin);
mEnterpriseQuery = (Button) findViewById(R.id.mEnterpriseQuery);
mLogin.setOnClickListener(this);
mEnterpriseQuery.setOnClickListener(this);
mLoginNumber.setText("15249243001");
mLoginPswd.setText("123456");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mLogin:
if (TextUtils.isEmpty(mLoginNumber.getText().toString())) {
ToastUtil.ShowCentre(LoginActivity.this, "请输入手机号码");
return;
}
if (TextUtils.isEmpty(mLoginPswd.getText().toString())) {
ToastUtil.ShowCentre(LoginActivity.this, "请输入登录密码");
return;
}
List<User> Stations = User.findWithQuery(User.class, "Select * from User where userPhone = " + mLoginNumber.getText().toString() + " and userPswd= " + mLoginPswd.getText().toString());
if (Stations.size()==0) {
CustomToast.showToast(LoginActivity.this, "用户名或密码错误");
}else{
MemberUserUtils.setUid(LoginActivity.this, Stations.get(0).getId()+"");
MemberUserUtils.setName(LoginActivity.this, Stations.get(0).getUserName());
MemberUserUtils.setPhone(LoginActivity.this, Stations.get(0).getUserPhone());
MemberUserUtils.setPswd(LoginActivity.this, Stations.get(0).getUserPswd());
Log.i("pony_log", "Stations:" + Stations.get(0).toString() + "");
Intent intent = new Intent(LoginActivity.this, FrameworkActivity.class);
startActivity(intent);
finish();
}
break;
case R.id.mEnterpriseQuery:
Intent mEnterpriseQuery = new Intent(this, RegisterActivity.class);
startActivity(mEnterpriseQuery);
break;
default:
break;
}
}
@Override
public void initData() {
}
}
3:代码说明
List<User> usrtList= User.findWithQuery(User.class, "Select * from User where userPhone = " + mLoginNumber.getText().toString() + " and userPswd= " + mLoginPswd.getText().toString());说明:这个是获取用户的手机号码和密码,进行用户的登录信息验证
if (usrtList.size()==0) { CustomToast.showToast(LoginActivity.this, "用户名或密码错误"); }else{ MemberUserUtils.setUid(LoginActivity.this, Stations.get(0).getId()+""); MemberUserUtils.setName(LoginActivity.this, Stations.get(0).getUserName()); MemberUserUtils.setPhone(LoginActivity.this, Stations.get(0).getUserPhone()); MemberUserUtils.setPswd(LoginActivity.this, Stations.get(0).getUserPswd()); Log.i("pony_log", "Stations:" + Stations.get(0).toString() + ""); Intent intent = new Intent(LoginActivity.this, FrameworkActivity.class); startActivity(intent); finish(); }说明:usrtList是通过用户手机号码和密码登录之后的返回数据,这边是判断假如数据存在,那就是登录成功;然后进行页面的跳转
上一篇:Android课程设计-记事本项目(1)-环境及软件_Android毕业设计源码的博客-CSDN博客
更多信息获取请关注微信公众号:“毕设code”