MainActivity : 倒计时
package com.example.yklz1024;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends BaseActivity {
private TextView countdown;
private Button btn_tz;
private int temp = 5;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
temp--;
if (temp <= 0){
startActivity(new Intent(MainActivity.this,LoginActivity.class));
finish();
}else {
countdown.setText(temp + "S");
handler.sendEmptyMessageDelayed(1,1000);
}
}
};
//加载视图
@Override
protected int setView() {
return R.layout.activity_main;
}
//初始化资源控件
protected void initViews() {
countdown = (TextView) findViewById(R.id.countdown);
btn_tz = (Button) findViewById(R.id.btn_tz);
}
//处理数据
@Override
protected void initData() {
handler.sendEmptyMessageDelayed(1, 1000);
//第一次进入显示倒计时页面 第二次进入时 直接进入登录页面 跳过倒计时页面
//得到SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("lz",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
boolean x = sharedPreferences.getBoolean("x",false);
if (x) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}else {
editor.putBoolean("x",true);
editor.commit();
}
//点击跳转按钮 跳转至登录页面
btn_tz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
handler.sendEmptyMessage(1);
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeMessages(1);
}
}
倒计时页面的布局
LoginActivity 登陆页面
package com.example.yklz1024;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class LoginActivity extends BaseActivity {
private EditText edit_name,edit_pwd;
private CheckBox check_jz,check_zd;
private Button btn_login;
private SharedPreferences sharedPreferences;
//加载视图
@Override
protected int setView() {
return R.layout.activity_login;
}
//获取资源id
protected void initViews() {
edit_name = findViewById(R.id.edit_name);
edit_pwd = findViewById(R.id.edit_pwd);
check_jz = findViewById(R.id.check_jz);
check_zd = findViewById(R.id.check_zd);
btn_login = findViewById(R.id.btn_login);
}
//加载数据
@Override
protected void initData() {
//获得sharepreferences
sharedPreferences = getSharedPreferences("lz",MODE_PRIVATE);
//设置回显数据
boolean isJZ = sharedPreferences.getBoolean("isJZ", false);
String username = sharedPreferences.getString("username","");
String password = sharedPreferences.getString("password", "");
if (isJZ){
edit_name.setText(username);
edit_pwd.setText(password);
check_jz.setChecked(true);
}
//设置自动登录
boolean isZD = sharedPreferences.getBoolean("isZD", false);
if (isZD){
startActivity(new Intent(LoginActivity.this,HomePageActivity.class));
finish();
}
//登录按钮点击事件 点击跳转至首页面
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取账号 密码
String username = edit_name.getText().toString();
String password = edit_pwd.getText().toString();
//是否选中记住密码
if (check_jz.isChecked()){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username",username);
editor.putString("password",password);
editor.putBoolean("isJZ",true);
editor.commit();
}
//是否选中自动登录
if (check_zd.isChecked()){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isZD",true);
editor.commit();
}
startActivity(new Intent(LoginActivity.this,HomePageActivity.class));
finish();
}
});
}
}
登录页面的布局
HomePageActivity 首页面 可注销登录
package com.example.yklz1024;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HomePageActivity extends BaseActivity {
private Button btn_zx;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
//加载视图
@Override
protected int setView() {
return R.layout.activity_home_page;
}
//初始化资源控件
protected void initViews() {
btn_zx = findViewById(R.id.btn_zx);
}
//加载数据
@Override
protected void initData() {
//得到sharedPreferences
sharedPreferences = getSharedPreferences("lz", MODE_PRIVATE);
editor = sharedPreferences.edit();
//注销按钮点击事件 点击返回登录界面
btn_zx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//清空登录信息
editor.clear();
//提交
editor.commit();
//页面跳转
startActivity(new Intent(HomePageActivity.this,LoginActivity.class));
finish();
}
});
}
}
首页面 布局 注销登录信息
抽取基类 BaseActivity
package com.example.yklz1024;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化视图
setContentView(setView());
//初始化资源控件
initViews();
//加载数据();
initData();
}
protected abstract int setView();
protected abstract void initViews();
protected abstract void initData();
}