3.boxuegu登录代码

视频课:http://stu.ityxb.com/preview/detail/fa522820dc1d4707bc1930506cbb708d
在这里插入图片描述

  1. 前端代码
    XML文件。
    android:background="@drawable/register_selector"//选择器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_login"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    tools:context="lesson10.computer.myviews.activity.LoginActivity">
    <include layout="@layout/main_title_bar"></include>
    <ImageView
        android:id="@+id/iv_head"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="25dp"
        android:src="@drawable/default_icon"
        android:layout_gravity="center"
        />
    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="@drawable/login_user_name_bg"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="35dp"
        android:drawableLeft="@drawable/user_name_icon"
        android:paddingLeft="15dp"
        android:hint="请输入用户名"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp"
        android:drawablePadding="10dp"
        android:singleLine="true"
        android:textColor="#000000"
        />
    <EditText
        android:id="@+id/et_psw"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="@drawable/login_psw_bg"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:drawableLeft="@drawable/psw_icon"
        android:paddingLeft="15dp"
        android:hint="请输入密码"
        android:textColorHint="#a3a3a3"
        android:textSize="14sp"
        android:drawablePadding="10dp"
        android:singleLine="true"
        android:textColor="#000000"
        />
    <Button
        android:id="@+id/btn_login"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:background="@drawable/register_selector"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_marginRight="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="8dp"
        >
        <TextView
            android:id="@+id/tv_register"
            android:gravity="center_horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="立即注册"
            android:layout_weight="1"
            android:padding="8dp"
            android:textColor="@android:color/white"
            android:textSize="14sp"
            />
        <TextView
            android:id="@+id/tv_find_psw"
            android:layout_width="0dp"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="找回密码?"
            android:padding="8dp"
            android:textColor="@android:color/white"
            android:textSize="14sp"
            />
    </LinearLayout>

</LinearLayout>

后端代码

    /**
     * 获取页面控件
     */
    private void init() {
        //标题
        tv_main_title = (TextView)findViewById(R.id.tv_main_title);
        tv_main_title.setText("登录");
        //返回键
        tv_back = (TextView) findViewById(R.id.tv_back);
        //立即注册
        tv_register = (TextView) findViewById(R.id.tv_register);
        //找回密码
        tv_find_psw = (TextView) findViewById(R.id.tv_find_psw);
        //登录按钮
        btn_login = (Button) findViewById(R.id.btn_login);
        //用户名的编辑文本
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        //密码的编辑文本
        et_psw = (EditText) findViewById(R.id.et_psw);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
            }
        });
        //立即注册的点击事件
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进行跳转界面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                //跳转后得出一个结果,得到一个返回值
                //从注册的界面回传数据到登录界面
                startActivityForResult(intent, 1);
            }
        });
        //找回密码控件的点击事件
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到找回密码界面(此页面暂未设置)
            }
        });

        //登录按钮的事件
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = et_user_name.getText().toString().trim();
                psw = et_psw.getText().toString().trim();
                String md5Psw = MD5Utils.md5(psw);
                String spPsw = readPsw(userName);
                if(TextUtils.isEmpty(userName)){
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(psw)){
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(md5Psw.equals(spPsw)){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //保存登录状态和登录的用户名
                    saveLoginStatus(true,userName);
                    Intent data = new Intent();
                    data.putExtra("isLogin",true);
                    //告诉前一个界面当前是ok的
                    setResult(RESULT_OK,data);
                    LoginActivity.this.finish();
                    return;
                }else if(!TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw)){
                    Toast.makeText(LoginActivity.this, "输入用户名和密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    //status是否登录成功的状态,userName:用户名
    private void saveLoginStatus(boolean status, String userName) {
        //LoginInfor表示文件名
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit(); //获取编辑器
        editor.putBoolean("isLogin", status);   //存入boolean类型的登录状态
        editor.putString("loginUserName", userName); //存入登陆时的用户名
        editor.commit();   //提交修改
    }

    private String readPsw(String userName) {
        //LoginInfo文件名,MODE_PRIVATE模式为私有的
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        return sp.getString(userName,"");
    }

    @Override
    //处理注册界面返回的数据可以用当前函数
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data!=null){
            //从注册界面传递过来的用户名
            String userName = data.getStringExtra("userName");
            //String psw = data.getStringExtra("psw");
            if(!TextUtils.isEmpty(userName)){
                et_user_name.setText(userName);
                //et_psw.setText(psw);
                //设置光标的位置
                et_user_name.setSelection(userName.length());
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值