一、主界面
1.样图
2.代码
登录Activity
package com.example.hotel;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hotel.Activity.Manager.ManagerActivity;
import com.example.hotel.Activity.RegisterActivity;
import com.example.hotel.Activity.NopwdActivity;
import com.example.hotel.Activity.User.UserActivity;
import com.example.hotel.Database.DBOpenHelper;
import com.example.hotel.Database.MysqlUser;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private TextView TextViewNoPassword;
private TextView TextViewRegister;
private TextView TextViewAppName;
private Button ButtonLogin;
private TextView EditTextLoginId;
private TextView EditTextLoginPassword;
private DBOpenHelper dbOpenHelper;
private static String correctpwd;
private static String power;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextViewAppName=(TextView) findViewById(R.id.textViewAppName);
TextViewAppName.setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(), "font/XingKai.TTF"));
// dbOpenHelper=new DBOpenHelper(MainActivity.this,"mydb",null,1);
//忘记密码
TextViewNoPassword=(TextView)findViewById(R.id.textViewNoPassword);
TextViewNoPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, NopwdActivity.class);
startActivity(intent);
}
});
//注册用户
TextViewRegister=(TextView) findViewById(R.id.textViewRegister);
TextViewRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
//登录
ButtonLogin=(Button) findViewById(R.id.buttonLogin);
EditTextLoginId=(TextView)findViewById(R.id.editTextLoginId);
EditTextLoginPassword=(TextView)findViewById(R.id.editTextLoginPassword);
EditTextLoginId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
new Thread(new Runnable() {
@Override
public void run() {
MysqlUser mysqlUser=new MysqlUser();
try {
String sql="select password from userinfo where id='"+EditTextLoginId.getText().toString()+"'";
correctpwd=mysqlUser.select(sql);
String sql2="select power from userinfo where id='"+EditTextLoginId.getText().toString()+"'";
power=mysqlUser.select(sql2);
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
});
ButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
// SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
// String Id=EditTextLoginId.getText().toString();
// String pwd=EditTextLoginPassword.getText().toString();
// Cursor cursor = db.rawQuery("select * from user",null);
// while (cursor.moveToNext()) {
// String searchName = cursor.getString(cursor.getColumnIndex("id"));
// String searchAge = cursor.getString(cursor.getColumnIndex("password"));
// if(searchName.equals(Id)&&searchAge.equals(pwd)){
// Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
// startActivity(intent);
// break;
// }
// }
}
});
// SharedPreferences sp=getSharedPreferences("hotel", Context.MODE_PRIVATE);
// String id=sp.getString("id",null);
// EditTextLoginId.setText(id);
}
private void login(){
String id=EditTextLoginId.getText().toString();
String pwd=EditTextLoginPassword.getText().toString();
Spinner spinner=(Spinner)findViewById(R.id.Spinner);
String ch=spinner.getSelectedItem().toString();
// SharedPreferences sp=this.getSharedPreferences("hotel",Context.MODE_PRIVATE);
// String password=sp.getString("pwd",null);
if(ch.equals("用户")){
if(pwd==null){
// Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
}
else {
if(power.equals("0")){
if(!pwd.equals(correctpwd)){
// Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
}
if(pwd.equals(correctpwd)&&power.equals("0")){
this.finish();
Intent intent= new Intent(MainActivity.this, UserActivity.class);
Bundle bundle=new Bundle();
bundle.putString("nowid",id);
intent.putExtras(bundle);
startActivity(intent);
}
}
else Toast.makeText(this,"您不能以用户登录",Toast.LENGTH_SHORT).show();
}
}
if(ch.equals("管理员")){
if(pwd==null){
// Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
}
else {
if(power.equals("1")){
if(!pwd.equals(correctpwd)&&power.equals("1")){
// Toast.makeText(this,"密码不正确",Toast.LENGTH_SHORT).show();
}
if(pwd.equals(correctpwd)){
this.finish();
Intent intent= new Intent(MainActivity.this, ManagerActivity.class);
startActivity(intent);
}
}
else Toast.makeText(this,"您不能以管理员登录",Toast.LENGTH_SHORT).show();
}
}
}
}
登录布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".MainActivity"
android:orientation="vertical"
android:background="@drawable/login">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="50dp"
android:textColor="#F44336"
android:textStyle="bold"
android:textSize="60sp"
android:typeface="serif"
android:text="入住通" />
<TextView
android:id="@+id/textViewLoginId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:text="用户id" />
<EditText
android:id="@+id/editTextLoginId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/corners"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<TextView
android:id="@+id/textViewLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="密码" />
<EditText
android:id="@+id/editTextLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/corners"
android:ems="10"
android:inputType="textPassword" />
<Spinner
android:id="@+id/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:entries="@array/ctype"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:background="@drawable/btnpress"
android:text="登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/textViewNoPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2196f3"
android:layout_marginRight="10dp"
android:text="忘记密码" />
<TextView
android:id="@+id/textViewRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2196f3"
android:text="注册用户" />
</LinearLayout>
</