###一、实现思路
登陆功能实现思路:
1、通过Java获取View页面的相关控件,并对需要的控件安装事件监听
2、登录前需要对当前用户进行检查,是否在本机上有过登陆状态记录,有过记录就读取本地的XML文件输入用户手机号和密码(上一次登陆有过"记住密码"操作的情况下)
3、本地登陆实现:
1)判断账号密码是否为空
2)显示加载框//保留项
3)开辟新线程进行登陆操作
禁止登陆按钮可用
线程睡眠1秒
向服务器发送请求进行信息核对
登陆成功提示并且保存用户登陆表单的信息和状态设置,并且进行View跳转后,finish()当前页面
登陆失败给予信息提示并且恢复登陆按钮可用和对加载框进行隐藏操作
###二、Android开发过程
1)清单文件AndroidManifest.xml的使用
2)layout下xml文件的构建(相当于HTML页面),实例:activity_main.xml和activity_login.xml,先把activity页面的XML构建出来先(相当于静态HTML)
3)页面的构建过程可能需要关联其他的XML文件或其他类型文件,例:页面设计的颜色统一放到res/values下的colors.xml;页面的静态字段统一放到res/values下的strings.xml;还有一个styles.xml文件专门用来配置主题风格,也就是不同设备的风格适配,以达到不同设备下可能存在的API版本不同的问题,通过在styles.xml下设置多种不同的主题风格,让APP自动识别系统API版本自动选择对应的主题风格达到同一APP在不同API版本系统下的显示的主题风格不同的效果;有些效果可能需要在drawable文件下进行绘制特定效果的XML文件,然后在layout文件下的activity_**.xml的文件中通过@drawable/repeat_bg的方式导入(repeat_bg其实是repeat_bg.xml文件,如果将整个文件作为一个activity的局部效果就可以通过这种方式导入,并且把后缀名去掉)
###三、代码实现
####3.1首先构建activity的第一个页面:activity_login.xml,activity_main.xml这个默认页面不作修改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/v_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="58px"
android:orientation="horizontal">
<ImageView
android:id="@+id/return_bt"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="40px"
android:contentDescription="@string/return_bt"
android:src="@drawable/return_bt" />
<ImageView
android:id="@+id/theme"
android:layout_width="264dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_marginLeft="30px"
android:contentDescription="@string/log_theme"
android:src="@drawable/theme" />
</LinearLayout>
<LinearLayout
android:id="@+id/v_2"
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal">
<TextView
android:id="@+id/pwd_menu"
android:layout_width="50dp"
android:layout_height="31dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="密码登陆"
android:textSize="24sp"
android:textStyle="bold"
tools:text="密码登陆" />
<View
android:id="@+id/divider2"
android:layout_width="4dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:layout_marginBottom="10dp"
android:background="?android:attr/listDivider" />
<TextView
android:id="@+id/vcode_menu"
android:layout_width="50dp"
android:layout_height="29dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:fontFamily=""
android:gravity="center"
android:text="验证码登陆"
android:textSize="24sp"
android:textStyle="bold"
tools:text="验证码登陆" />
</LinearLayout>
<LinearLayout
android:id="@+id/v_3"
android:layout_width="match_parent"
android:layout_height="102dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/v_3_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/input_bg"
android:orientation="horizontal">
<ImageView
android:id="@+id/phone_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/phone" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="输入手机号码"
android:inputType="phone"
android:selectAllOnFocus="true"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/v_3_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/repeat_bg"
android:orientation="horizontal">
<ImageView
android:id="@+id/pwd_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/pwd" />
<EditText
android:id="@+id/passwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="输入登录密码"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/v_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/remember_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住密码" />
<ImageButton
android:id="@+id/login_iButton"
android:layout_width="match_parent"
android:layout_height="227dp"
android:src="@drawable/login_bt" />
</LinearLayout>
</LinearLayout>
页面中主要的控件有四个,两个EditText用来输入账号和密码,一个CheckBox用来记住密码,一个ImageButton用来点击登陆提交。可以通过拖拽来完成,如果Android Studio的设计区图形界面没显示可以到res/values下的styles.xml看一下
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
将Theme.AppCompat.Light.DarkActionBar修改为Base.Theme.AppCompat.Light.DarkActionBar就可以了(可能是这种情况)
####3.2MainActivity.java代码的实现
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login();
}
//1、启动登陆的Activity页面
public void login() {
startActivity(new Intent(this, LoginActivity.class));
}
}
####3.3LoginActivity.java代码的实现
package com.ljj.administrator.vitah1.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.ljj.administrator.vitah1.R;
import com.ljj.administrator.vitah1.util.Base64Utils;
import com.ljj.administrator.vitah1.util.SharedPreferencesUtils;
public class LoginActivity extends Activity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
//布局内的控件
private ImageView return_bt;
private TextView pwd_menu;
private TextView vcode_menu;
private EditText phone;
private EditText passwd;
private ImageButton login_bt;
private CheckBox checkBox_password;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews(); //初始化视图
setEvents(); //设置事件
initData(); //初始化数据
}
private void initViews() {
return_bt = (ImageView)findViewById(R.id.return_bt);
pwd_menu = (TextView)findViewById(R.id.pwd_menu);
vcode_menu = (TextView)findViewById(R.id.vcode_menu);
phone = (EditText)findViewById(R.id.phone);
passwd = (EditText)findViewById(R.id.passwd);
login_bt = (ImageButton)findViewById(R.id.login_iButton);
checkBox_password = (CheckBox) findViewById(R.id.remember_pwd);
}
private void setEvents(){
return_bt.setOnClickListener(this);
pwd_menu.setOnClickListener(this);
vcode_menu.setOnClickListener(this);
login_bt.setOnClickListener(this);
checkBox_password.setOnCheckedChangeListener(this);
}
private void initData() {
//判断用户第一次登陆
if(firstLogin()) {
checkBox_password.setChecked(false);//取消记住密码的复选框
}
//判断是否记住密码
if(remenberPassword()) {
checkBox_password.setChecked(true);
setTextPhoneAndPassword();//把phone和password输入到输入框中
}else{
setTextPhone();//把账号放到输入框
}
}
/**
* 点击事件监听
* @param v
*/
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.login_iButton:
loadUserPhone(); //保存一下用户名手机号
login(); //登陆
break;
}
}
/**
* 将本地保存的账号密码输入到输入框中
*/
public void setTextPhoneAndPassword() {
phone.setText("" + getLocalPhone());
passwd.setText("" + getLocalPassword());
}
/**
* 设置数据到输入框中
*/
public void setTextPhone(){
phone.setText("" + getLocalPhone());
}
/**
* 获取保存在本地的手机号
* @return
*/
public String getLocalPhone() {
SharedPreferencesUtils helper = new SharedPreferencesUtils(this,"setting");
String phone = helper.getString("phone");
return phone;
}
/**
* 获取保存在本地的密码
* @return
*/
public String getLocalPassword() {
SharedPreferencesUtils helper = new SharedPreferencesUtils(this,"setting");
String password = helper.getString("password");
return Base64Utils.decryptBASE64(password);//解密
}
/**
* 本地登陆
* 用户名:13022066704,密码:123456
*/
private void login() {
if(getPhone().isEmpty()){
showToast("你输入的账号为空");
return;
}
if(getPassword().isEmpty()){
showToast("你输入的密码为空");
}
//请求服务器账号密码是否正确,网络请求,子线程
// showLoading(); //显示加载框
Thread loginRunable = new Thread(){
@Override
public void run() {
super.run();
setLoginBtnClickable(false);//登录过程中登录按钮不可点击
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//判断账号和密码
if(getPhone().equals("13022066704")&&getPassword().equals("123456")){
showToast("登陆成功");
loadCheckBoxState();//登陆成功保存一下用户的登陆前的状态
//登陆成功后跳转至LoginAfterActivity.class
// startActivity(new Intent(LoginActivity.this,LoginAfterActivity.class));
finish();//关闭当前页面
} else {
showToast("输入的号码或密码不正确");
}
setLoginBtnClickable(true);//登录失败恢复登录按钮可用
// hideLoading();//隐藏加载框
}
} ;
loginRunable.start();//单独开辟一条线程来进行登录操作
}
/**
* 保存用户选择"记住密码"的状态
*/
private void loadCheckBoxState() {
loadCheckBoxState(checkBox_password);
}
/**
* 保存按钮的状态值
* @param checkBox_password
*/
public void loadCheckBoxState(CheckBox checkBox_password){
SharedPreferencesUtils helper = new SharedPreferencesUtils(this,"setting");
//如果设置了记住密码
if(checkBox_password.isChecked()){
helper.putValues(
new SharedPreferencesUtils.ContentValue("remenberPassword",true),
new SharedPreferencesUtils.ContentValue("password",Base64Utils.encryptBASE64(getPassword()))
);
} else { //取消记住密码时
helper.putValues(
new SharedPreferencesUtils.ContentValue("remenberPassword",false),
new SharedPreferencesUtils.ContentValue("password","")
);
}
}
/**
* 保存一下用户手机号
*/
public void loadUserPhone() {
if(!getPhone().equals("")||!getPhone().equals("输入手机号码")){
SharedPreferencesUtils helper = new SharedPreferencesUtils(this,"setting");
helper.putValues(new SharedPreferencesUtils.ContentValue("phone",getPhone()));
}
}
/**
* 获取手机号
* @return
*/
public String getPhone() {
return phone.getText().toString().trim();//去掉空格
}
/**
* 获取密码
* @return
*/
public String getPassword() {
return passwd.getText().toString().trim();
}
/**
* 是否可以点击登陆按钮
*
* @param clickable
*/
public void setLoginBtnClickable(boolean clickable) {
login_bt.setClickable(clickable);
}
/**
*CheckBox点击时的回调方法
*
* @param buttonView 按钮对象
* @param isChecked 按钮的状态
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (buttonView == checkBox_password) { //记住密码选框发生改变时
if (!isChecked) {
//逻辑填充
}
}
}
@Override
public void onBackPressed() {
}
public void showToast(final String msg){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
SharedPreferences部分不上代码了,本地有三种方法存储数据,SQLite、Acache、SharePreferences,会那种就用哪种吧