zoukankan      html  css  js  c++  java
  • 第一次冲刺(四)

    今天我完成了登录界面的activity。

    代码为:

    package com.example.hotel;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    import com.example.hotel.Activity.Manager.ManagerActivity;
    import com.example.hotel.Activity.Manager.ManchoiceActivity;
    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 CheckBox Rem;
        private CheckBox Auto;
        private DBOpenHelper dbOpenHelper;
        private static String correctpwd;
        private static String power;
        private String searchid,id,pwd;
        private boolean rem,auto;
        private int loginfirst;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    //        SharedPreferences sp=getSharedPreferences("login", Context.MODE_PRIVATE);
    //        loginfirst=sp.getInt("loginfirst",1);
    //        loginfirst=1;
    //        if(loginfirst==0){
    //            SharedPreferences sp1=getSharedPreferences("user", Context.MODE_PRIVATE);
    //            String str=sp1.getString("nowid",null);
    //            Intent intent= new Intent(MainActivity.this, UserActivity.class);
    //            Bundle bundle=new Bundle();
    //            bundle.putString("nowid",str);
    //            intent.putExtras(bundle);
    //            startActivity(intent);
    //        }
            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) {
                    Thread thread=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();
                            }
                        }
                    });
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            ButtonLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Thread thread=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            MysqlUser mysqlUser=new MysqlUser();
                            try {
                                String sql="select * from userinfo where id='"+EditTextLoginId.getText().toString()+"'";
                                searchid=mysqlUser.select(sql);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    thread.start();
                    try {
                        thread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    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("user", Context.MODE_PRIVATE);
    //        Id = sp.getString("id", "");
    //        EditTextLoginId.setText(Id);
    //        if (sp!= null) {
    //            Id = sp.getString("id", "");
    //            Pwd= sp.getString("pwd", "");
    //            rem = sp.getBoolean("remenber", false);
    //            auto = sp.getBoolean("auto", false);
    //            EditTextLoginId.setText(Id);
    //        }
    //        if (rem) {
    //            Rem.setChecked(true);
    //            EditTextLoginPassword.setText(Pwd);
    //        }
    //        //选择了自动登录后直接登录
    //        if (auto){
    //            Auto.setChecked(true);
    //            login();
    //        }
    //        Rem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    //            @Override
    //            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //                if (!isChecked){
    //                    Rem.setChecked(false);
    //                    //清空密码输入框
    //                    EditTextLoginPassword.setText("");
    //                }
    //            }
    //        });
    //        Auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    //            @Override
    //            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //                if (!isChecked){
    //                    Auto.setChecked(false);
    //                }
    //            }
    //        });
        }
    
    
    
        private void login(){
            id=EditTextLoginId.getText().toString();
            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(id.equals("")){
                Toast.makeText(this,"id不能为空",Toast.LENGTH_SHORT).show();
                return;
            }
            if(searchid==null){
                Toast.makeText(this,"id不存在",Toast.LENGTH_SHORT).show();
                return;
            }
            if(ch.equals("用户")){
                if(pwd.equals("")){
                    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")){
    //                        SharedPreferences sp=this.getSharedPreferences("login",Context.MODE_PRIVATE);
    //                        SharedPreferences.Editor editor=sp.edit();
    //                        editor.putInt("loginfirst",0);
    //                        editor.apply();
    //                        SharedPreferences sp1=this.getSharedPreferences("user",Context.MODE_PRIVATE);
    //                        SharedPreferences.Editor editor1=sp1.edit();
    //                        editor.putString("nowid",id);
    //                        editor.apply();
                            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.equals("")){
                    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, ManchoiceActivity.class);
                            startActivity(intent);
                        }
                    }
                    else Toast.makeText(this,"您不能以管理员登录",Toast.LENGTH_SHORT).show();
                }
            }
        }
    
    }
  • 相关阅读:
    Java中读取.properties配置文件的通用类
    静态工厂方法中单例的延迟加载技术
    AVL树C++实现
    初探Java反射机制
    项目中一个 1 毫秒引发的问题
    用Java操作数据库Datetime数据
    【转】Linux中的EAGAIN含义
    【转】关于编译链接——gcc/g++
    《Linux多线程编程手册》读书笔记
    《Linux多线程服务端编程——使用muduo C++网络库》读书笔记
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910558.html
Copyright © 2011-2022 走看看