十天冲刺之三
在今天的学习中主要是对登录界面的编写
首先在layout中创建一个.xml其中对注册界面进行布局,
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!--登录界面-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<!--标题栏-->
<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:layout_gravity="center_horizontal"
android:background="@drawable/default_icon"/>
<EditText
android:id="@+id/et_user_name"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/login_user_name_bg"
android:drawableLeft="@drawable/user_name_icon"
android:drawablePadding="10dp"
android:paddingLeft="8dp"
android:gravity="center_vertical"
android:hint="请输入用户名"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#a3a3a3"
android:textSize="14sp"/>
<EditText
android:id="@+id/et_psw"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="@drawable/login_psw_bg"
android:drawableLeft="@drawable/psw_icon"
android:drawablePadding="10dp"
android:paddingLeft="8dp"
android:gravity="center_vertical"
android:hint="请输入密码"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#a3a3a3"
android:textSize="14sp"/>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal"
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_marginTop="8dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="立即注册"
android:textColor="@android:color/white"
android:textSize="14sp" />
<!--layout_weight="1" layout_width="0dp"实现均分效果-->
<TextView
android:id="@+id/tv_find_psw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="找回密码?"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
在实现代码功能时,首先声明了各控件属性
//标题 private TextView tv_main_title; //返回键,显示的注册,找回密码 private TextView tv_back,tv_register,tv_find_psw; //登录按钮 private Button btn_login; //获取的用户名,密码,加密密码 private String userName,psw,spPsw; //用户名和密码的输入框 private EditText et_user_name,et_psw;
创建初始化方法init(),用于获取登录界面所要用的空间,并设置登录按钮、返回按钮、立刻注册、找回密码点击事件
//获取界面控件 private void init() { //从main_title_bar中获取的id tv_main_title=findViewById(R.id.tv_main_title); tv_main_title.setText("登录"); tv_back=findViewById(R.id.tv_back); //从activity_login.xml中获取的 tv_register=findViewById(R.id.tv_register); tv_find_psw=findViewById(R.id.tv_find_psw); btn_login=findViewById(R.id.btn_login); et_user_name=findViewById(R.id.et_user_name); et_psw=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) { //跳转到找回密码界面(此页面暂未创建) /*Intent intent=new Intent(LoginActivity.this,FindPwdActivity.class); startActivity(intent);*/ } }); //登录按钮的点击事件 btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //开始登录,获取用户名和密码 getText().toString().trim(); userName=et_user_name.getText().toString().trim(); psw=et_psw.getText().toString().trim(); //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致 String md5Psw= MD5Utils.md5(psw); // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码 // 定义方法 readPsw为了读取用户名,得到密码 spPsw=readPsw(userName); // TextUtils.isEmpty 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; // md5Psw.equals(); 判断,输入的密码加密后,是否与保存在SharedPreferences中一致 }else if(md5Psw.equals(spPsw)){ //一致登录成功 Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); //保存登录状态,在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名; saveLoginStatus(true, userName); //登录成功后关闭此页面进入主页 Intent data=new Intent(); //datad.putExtra( ); name , value ; data.putExtra("isLogin",true); data.putExtra("userName",userName); //RESULT_OK为Activity系统常量,状态码为-1 // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值 setResult(RESULT_OK,data); //销毁登录界面 LoginActivity.this.finish(); //跳转到主界面,登录成功的状态传递到 MainActivity 中 //startActivity(new Intent(LoginActivity.this, MainActivity.class)); return; }else if((spPsw!=null&&!TextUtils.isEmpty(spPsw)&&!md5Psw.equals(spPsw))){ Toast.makeText(LoginActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show(); return; }else{ Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show(); } } }); }