1. 程式人生 > >倒計時 Handle+Timer 使用sharedPreferences登入 登出

倒計時 Handle+Timer 使用sharedPreferences登入 登出

MainActivity : 倒計時

package com.example.yklz1024;

import android.content.Intent;
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 = findViewById(R.id.countdown);
        btn_tz = findViewById(R.id.btn_tz);
    }

    //處理資料
    @Override
    protected void initData() {
        handler.sendEmptyMessageDelayed(1, 1000);
        //點選跳轉按鈕 跳轉至登入頁面
        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);

    }

}

倒計時頁面的佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/countdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="21dp"
        android:layout_marginTop="16dp"
        android:text="倒計時" />

    <Button
        android:id="@+id/btn_tz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="12dp"
        android:text="點選跳轉" />
</RelativeLayout>

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();
            }
        });

    }


}

登入頁面的佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">

    <EditText
        android:id="@+id/edit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="127dp"
        android:hint="請輸入賬號" />

    <EditText
        android:id="@+id/edit_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/edit_name"
        android:layout_marginTop="200dp"
        android:hint="請輸入密碼" />

    <CheckBox
        android:id="@+id/check_jz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="80dp"
        android:text="記住密碼" />

    <CheckBox
        android:id="@+id/check_zd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="91dp"
        android:text="自動登入" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="175dp"
        android:text="登入" />

</RelativeLayout>

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();
            }
        });
    }
}

首頁面 佈局 登出登入資訊

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomePageActivity">

    <Button
        android:id="@+id/btn_zx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登出"/>

</RelativeLayout>

抽取基類 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();
}