使用中介者模式管理的登錄UI模塊

下面我們開發一個登錄界面,界面中使用中介者矛盾管理界面中的控件。

1 首先創建登錄界面的佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wang.mediatorpatternlogindemo.LoginActivity">

    <EditText
        android:id="@+id/etAccount"
        android:hint="請輸入賬號"
        android:padding="8dip"
        android:layout_marginRight="12dip"
        android:layout_marginLeft="12dip"
        android:layout_marginTop="24dip"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/etPassword"
        android:hint="請輸入密碼"
        android:padding="8dip"
        android:layout_marginRight="12dip"
        android:layout_marginLeft="12dip"
        android:layout_marginTop="24dip"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/cbRemember"
            android:text="記住賬號"
            android:textSize="14sp"
            android:layout_marginTop="16dip"
            android:layout_marginLeft="12dip"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/cbAutoLogin"
            android:text="自動登錄"
            android:textSize="14sp"
            android:layout_marginTop="16dip"
            android:layout_marginRight="12dip"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <RelativeLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/btConfirm"
        android:textSize="16sp"
        android:text="確定"
        android:paddingBottom="6dip"
        android:paddingTop="6dip"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btCancel"
        android:textSize="16sp"
        android:text="取消"
        android:paddingBottom="6dip"
        android:paddingTop="6dip"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        android:layout_alignParentRight="true"
        android:layout_marginRight="12dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>

</LinearLayout>

2  在 activity中設置界面交互和業務邏輯:

 

package com.wang.mediatorpatternlogindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText etAccount, etPassword;
    private CheckBox cbRemember, cbAutoLogin;
    private Button btConfirm, btCancel;
    //賬號信息
    private String mAccount, mPwd;
    //是否記住賬號, 自動登錄
    private boolean isRemember, isAuto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView() {
        etAccount = findViewById(R.id.etAccount);
        etPassword = findViewById(R.id.etPassword);
        cbAutoLogin = findViewById(R.id.cbAutoLogin);
        cbRemember = findViewById(R.id.cbRemember);
        btCancel = findViewById(R.id.btCancel);
        btConfirm = findViewById(R.id.btConfirm);

    }

    private void initEvent() {

        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                mAccount = s.toString();
                //通知給activity
                change();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etPassword.setEnabled(false);
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPwd = s.toString();
                change();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        btConfirm.setEnabled(false);
        btConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "用戶登錄成功", Toast.LENGTH_SHORT).show();
            }
        });

        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "取消登錄", Toast.LENGTH_SHORT).show();

            }
        });
        cbAutoLogin.setEnabled(false);
        cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isAuto = isChecked;
                change();
            }
        });
        cbRemember.setEnabled(false);
        cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isRemember = isChecked;
                change();
            }
        });
    }

    /**
     * Activity是中介者,change作爲管理控件找的中介者方法
     */
    private void change() {
        if (mAccount.isEmpty()) {
            etPassword.setEnabled(false);

            cbAutoLogin.setEnabled(false);
            cbRemember.setEnabled(false);

            btConfirm.setEnabled(false);
            btCancel.setEnabled(true);
        } else if (!TextUtils.isEmpty(mAccount) && TextUtils.isEmpty(mPwd)) {
            etPassword.setEnabled(true);

            cbAutoLogin.setEnabled(true);
            cbRemember.setEnabled(true);

            btCancel.setEnabled(true);
            btConfirm.setEnabled(false);
        } else if (!TextUtils.isEmpty(mAccount) && !TextUtils.isEmpty(mPwd)) {
            etPassword.setEnabled(true);

            cbRemember.setEnabled(true);
            cbAutoLogin.setEnabled(true);

            btCancel.setEnabled(true);
            btConfirm.setEnabled(true);
        }
    }
}

界面UI效果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章