EClass课堂项目(一)-登录注册

登录注册功能实现

登录实现

LoginActivity(登录活动)
(PS.因为是对前期项目的整理,因此含有部分其他代码,当时未学习注解使用,最好使用注解对View实现实例创建,以及对业务逻辑代码进行分离,比如此处的教师学生判断,登录进行的数据库判断,降低耦合性,更好实现MVC模型,之后有时间会进行重构)
代码主要包含几部分:
①输入用户名和密码,与数据库中的用户名密码进行比对(这里采用的是Bmob后端云数据库,详细使用参见官方SDK文档)
若未注册则可点击跳转注册界面
②判断是学生还是教师(通过注册填的学号或教师号),学生跳转学生主界面,教师跳转教师主界面
③登录成功播放登录动画(这部分后面会写一节)
④备选是否记住用户名和密码


/*
程序启动时显示的第一个活动界面,即为登陆页面
 */
public class LoginActivity extends AppCompatActivity {


    //用户名文本编辑框
    private EditText username;
    //密码文本编辑框
    private EditText password;


    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    private CheckBox rememberPass;

    private TransitionView mAnimView;


    //用来判断是否成功匹配的标志(很重要,若直接for循环内加上if会输出多次结果)
    public boolean flag1 = false;

    public String studentid2 = "";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //将正在创建的活动添加到活动管理器里
        ActivityCollector.addActivity(this);
        //设置布局
        setContentView(R.layout.login_layout);

        //默认初始化
        Bmob.initialize(this, "8790673e88e63fbe66a2a39a9339d15a");

        //获取SharedPreferences对象
        pref = PreferenceManager.getDefaultSharedPreferences(this);



        //得到记住密码的勾选框
        rememberPass = (CheckBox)findViewById(R.id.remember_pass);

        //判断是否选中记住密码
        boolean isRemember = pref.getBoolean("remember_password",false);

        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);


        if (isRemember){
            //将用户名和密码都设置到文本框中
            String Username = pref.getString("Username","");
            String Password = pref.getString("Password","");
            username.setText(Username);
            password.setText(Password);
            rememberPass.setChecked(true);
        }




        TextView login = (TextView) findViewById(R.id.tv_sign_up);
        //给登录设置监听器
        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {


                //Bmob遍历查询所有数据的方法
                BmobQuery query = new BmobQuery();
                query.findObjects(new FindListener() {
                    @Override
                    public void done(List list, BmobException e) {
                        for (User user : list) {
                            //判断用户输入的用户名和密码是否与数据库中相同,必须要有toString()
                            if (user.getUsername().equals(username.getText().toString()) &&
                                    user.getPassword().equals(password.getText().toString())) {
                                //匹配成功,flag为true
                                flag1 = true;
                                if(flag1){


                                    editor = pref.edit();


                                    if (rememberPass.isChecked()) {//检查复选框是否被选中
                                        editor.putBoolean("remember_password", true);
                                        editor.putString("Username", username.getText().toString());
                                        editor.putString("Password", password.getText().toString());
                                    }else {
                                        editor.clear();
                                    }

                                    if (isteacher(username.getText().toString())){//判断用户是否为教师,是跳转到教师页面
                                        editor.apply();
                                        //动画启动并跳转教师界面
                                        mAnimView.startAnimation();
                                    }
                                    else {//跳转学生页面
                                        editor.apply();
                                        //动画启动并跳转学生界面
                                        mAnimView.startAnimation();
                                    }
                                }else{
                                    //登录信息错误,通过Toast显示提示信息
                                    Toast.makeText(LoginActivity.this,"用户登录信息错误" , Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                });
            }
        });

        TextView regist = (TextView) findViewById(R.id.regist);
        regist.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
        regist.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });

        //动画实例
        mAnimView = (TransitionView) findViewById(R.id.ani_view);
        mAnimView.setOnAnimationEndListener(new TransitionView.OnAnimationEndListener()
        {
            @Override
            public void onEnd()
            {

                if (isteacher(username.getText().toString())){//判断用户是否为教师
                    gotoTActivity();
                }else {
                    gotoSActivity();
                }
            }
        });
    }


    //判断是否为教师
    public boolean isteacher(String Username){

        //通过User的用户名获取学号
        BmobQuery userBmobQuery = new BmobQuery();
        userBmobQuery.addWhereEqualTo("username",Username);
        userBmobQuery.findObjects(new FindListener() {
            @Override
            public void done(List list, BmobException e) {
                for(User user : list) {
                    studentid2 = user.getStudentId();
                }
            }
        });

        if (studentid2.length()==0){//如果该用户学号部分为空,说明其有教师编号,不是学生
            return true;
        }
        return false;
    }

    private void gotoTActivity()//跳转教师
    {
        String data2 = username.getText().toString();
        Intent intent = new Intent(LoginActivity.this,TeacherActivity.class);
        intent.putExtra("user_name",data2);
        startActivity(intent);
        finish();
    }

    private void gotoSActivity()//跳转学生
    {
        String datax = username.getText().toString();
        Intent intent = new Intent(LoginActivity.this,IndexActivity.class);
        intent.putExtra("user_name",datax);
        startActivity(intent);
        finish();
    }

    //表明一个要销毁的活动从活动管理器里移除
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    //创建Option菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    //定义菜单响应事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.exit://当点击了退出时,退出程序
                //销毁所有活动
                ActivityCollector.finishAll();
                //杀掉当前进程
                android.os.Process.killProcess(android.os.Process.myPid());
                break;
            default:
        }
        return true;
    }
}

对应的布局文件login_layout.xml



<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="@drawable/back2"
    >
    
    <TextView
        android:id="@+id/title"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/username"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="69dp"
        android:gravity="center"
        android:text="EClass"
        android:textColor="@color/colorWrite"
        android:textSize="35sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textColor="@color/colorWrite"
        android:textSize="18sp"
        android:layout_above="@+id/user_password"
        android:layout_toLeftOf="@+id/remember_pass"
        android:layout_toStartOf="@+id/remember_pass"
        android:layout_marginBottom="16dp" />
    

    <TextView
        android:id="@+id/user_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title"
        android:layout_centerVertical="true"
        android:text="Password"
        android:textColor="@color/colorWrite"
        android:textSize="18sp" />

    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:id="@+id/username"
        android:textColor="@color/colorWrite"
        android:digits="@string/user_name"
        android:maxLines="1"
        android:layout_alignBaseline="@+id/user_name"
        android:layout_alignBottom="@+id/user_name"
        android:layout_alignLeft="@+id/password"
        android:layout_alignStart="@+id/password" />
    




    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:id="@+id/password"
        android:digits="@string/pass_word"
        android:textColor="@color/colorWrite"
        android:maxLines="1"
        android:layout_marginRight="24dp"
        android:layout_marginEnd="24dp"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/title"
        android:layout_alignEnd="@+id/title" />

    <TextView

        android:layout_width="150dp"
        android:layout_height="40dp"
        android:gravity="center"
        android:textSize="24sp"
        android:text="LOGIN"
        android:textColor="@color/colorWrite"
        android:id="@+id/tv_sign_up"
        android:background="@drawable/selector_btn_bg"
        android:layout_marginBottom="85dp"
        android:layout_above="@+id/regist"
        android:layout_centerHorizontal="true" />


    <TextView
        android:id="@+id/regist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to SIGN UP"
        android:textColor="@color/btn_press"
        android:textSize="15sp"
        android:layout_marginRight="19dp"
        android:layout_marginEnd="19dp"
        android:layout_marginBottom="64dp"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/password"
        android:layout_alignEnd="@+id/password" />




    <CheckBox
        android:id="@+id/remember_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_toLeftOf="@+id/textView2"
        android:layout_toStartOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remember password"
        android:textColor="@color/colorWrite"
        android:textSize="14sp"
        android:layout_alignBaseline="@+id/remember_pass"
        android:layout_alignBottom="@+id/remember_pass"
        android:layout_alignRight="@+id/tv_sign_up"
        android:layout_alignEnd="@+id/tv_sign_up" />


    <com.example.eclass.Tools.TransitionView
        android:id="@+id/ani_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
RelativeLayout>

EClass课堂项目(一)-登录注册_第1张图片

注册实现

RegistActivity
功能:
①用户输入信息添加进入数据库中
②判断数据库中是否有同用户名或者学号/教师号
③添加输入字符限制或者输入完整性提示
④注册成功跳转回登录界面

public class RegistActivity extends AppCompatActivity {


    public String strusername;
    public String strpassword;
    public String strstudentid="";
    public String strteacherid="";
    public boolean flag1 = false;
    public boolean flag2 = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //将一个正在创建的活动添加到活动管理器中
        ActivityCollector.addActivity(this);
        setContentView(R.layout.regist_layout);

        //默认初始化
        Bmob.initialize(this, "8790673e88e63fbe66a2a39a9339d15a");

        Button yes = (Button) findViewById(R.id.YES);
        final Button no =(Button) findViewById(R.id.NO);



        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText username = (EditText)findViewById(R.id.username);
                EditText password = (EditText)findViewById(R.id.password);
                final EditText studentid = (EditText)findViewById(R.id.studentid);
                EditText teacherid = (EditText)findViewById(R.id.teacherid);
                strusername = username.getText().toString();
                strpassword = password.getText().toString();
                strstudentid = studentid.getText().toString();
                strteacherid = teacherid.getText().toString();

                //判断用户注册是否输入了密码和用户名,以及学号
                //首先学会如何判断EditText中内容为空,要包含trim(去掉首部空格)
                if ((strusername.length()!=0 && strpassword.length()!=0 && strstudentid.length()!=0)||
                        (strusername.length()!=0 && strpassword.length()!=0 && strteacherid.length()!=0)
                        ){
                    //若为学生
                    if(strstudentid.length() != 0) {
                        BmobQuery userBmobQuery = new BmobQuery();
                        userBmobQuery.findObjects(new FindListener() {
                            @Override
                            public void done(List list, BmobException e) {
                                for (User user : list) {
                                    //遍历User表,看是否含有相同的用户名和学号
                                    if (strusername.equals(user.getUsername()) || strstudentid.equals(user.getStudentId()))
                                    {
                                        flag1 = true;
                                    }
                                }
                                if(flag1) {
                                    Toast.makeText(RegistActivity.this, "用户名或学号已经被注册", Toast.LENGTH_SHORT).show();
                                }else {
                                    createUser();
                                }
                            }
                        });
                    }else if (strteacherid.length() != 0){//若为教师
                        BmobQuery userBmobQuery = new BmobQuery();
                        userBmobQuery.findObjects(new FindListener() {
                            @Override
                            public void done(List list, BmobException e) {
                                for (User user : list) {
                                    //遍历User表,看是否有相同用户名或教师号存在
                                    if (strusername.equals(user.getUsername()) || strteacherid.equals(user.getTeacherId()))
                                    {
                                        flag2 =true;
                                    }
                                }
                                if(flag2){
                                    Toast.makeText(RegistActivity.this, "用户名或教师号已经被注册", Toast.LENGTH_SHORT).show();
                                }else {
                                    createUser();
                                }
                            }

                        });
                    }

                }else {
                    Toast.makeText(RegistActivity.this,"用户名和密码不能为空,且学号和教师号必须输入一个" , Toast.LENGTH_SHORT).show();
                }

            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    //添加用户信息
    public void createUser() {
        User user1 = new User();
        //存储用户名密码
        user1.setUsername(strusername);
        user1.setPassword(strpassword);
        user1.setStudentId(strstudentid);
        user1.setTeacherId(strteacherid);
        user1.save(new SaveListener() {
            @Override
            public void done(String s, BmobException e) {
                Toast.makeText(RegistActivity.this, "创建用户成功,即将跳转回登录页面重新登录", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }



    //表明一个要销毁的活动从活动管理器里移除
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }


    //创建Option菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    //定义菜单响应事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.exit://当点击了退出时,退出程序
                ActivityCollector.finishAll();
                android.os.Process.killProcess(android.os.Process.myPid());
                break;
            default:
        }
        return true;
    }
}

regist_layout.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    >
    
    <TextView
        android:id="@+id/title"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:layout_marginTop="13dp"
        android:gravity="center"
        android:text="Sign Up"
        android:textColor="@color/btn_press"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="用户名"
        android:textColor="@color/btn_press"
        android:layout_alignBaseline="@+id/username"
        android:layout_alignBottom="@+id/username"
        android:layout_alignRight="@+id/user_password"
        android:layout_alignEnd="@+id/user_password" />
    

    <TextView
        android:id="@+id/user_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="密码"
        android:textColor="@color/btn_press"
        android:layout_marginRight="33dp"
        android:layout_marginEnd="33dp"
        android:layout_alignBaseline="@+id/password"
        android:layout_alignBottom="@+id/password"
        android:layout_toLeftOf="@+id/password"
        android:layout_toStartOf="@+id/password" />

    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWrite"
        android:textSize="20dip"
        android:id="@+id/username"
        android:textColorHint="@color/colorWrite"
        android:layout_marginTop="31dp"
        android:layout_below="@+id/title"
        android:layout_alignLeft="@+id/password"
        android:layout_alignStart="@+id/password"
        android:maxLines="1"
        android:hint="username"/>
    




    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="@color/colorWrite"
        android:id="@+id/password"
        android:layout_marginRight="62dp"
        android:layout_marginEnd="62dp"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/username"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLines="1"
        android:hint="password"
        android:textColorHint="@color/colorWrite"/>
    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20dip"
        android:text="确定"
        android:textColor="@color/colorWrite"
        android:id="@+id/YES"
        android:background="@color/btn_press"
        android:layout_marginBottom="46dp"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/user_password"
        android:layout_alignStart="@+id/user_password"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/NO"
        android:textSize="20dip"
        android:text="取消"
        android:background="@color/btn_press"
        android:textColor="@color/colorWrite"
        android:layout_alignBaseline="@+id/YES"
        android:layout_alignBottom="@+id/YES"
        android:layout_alignRight="@+id/password"
        android:layout_alignEnd="@+id/password" />

    <EditText
        android:id="@+id/studentid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入你的学号"
        android:textColor="@color/colorWrite"
        android:textColorHint="@color/colorWrite"
        android:maxLines="1"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/password"
        android:layout_alignStart="@+id/password" />
    <TextView
        android:text="学号"
        android:textSize="20dip"
        android:textColor="@color/btn_press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/user_password"
        android:layout_alignStart="@+id/user_password" />

    <TextView
        android:textSize="20dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="教师编号"
        android:textColor="@color/btn_press"
        android:layout_alignBaseline="@+id/teacherid"
        android:layout_alignBottom="@+id/teacherid"
        android:layout_alignLeft="@+id/user_name"
        android:layout_alignStart="@+id/user_name" />

    <EditText
        android:id="@+id/teacherid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入教师编号"
        android:textColor="@color/colorWrite"
        android:textColorHint="@color/colorWrite"
        android:layout_below="@+id/studentid"
        android:layout_alignLeft="@+id/studentid"
        android:layout_alignStart="@+id/studentid"
        android:layout_marginTop="16dp" />



RelativeLayout>

EClass课堂项目(一)-登录注册_第2张图片

你可能感兴趣的:(个人项目整理)