Android MVP mode for component and business logic separation

Source: Internet
Author: User

1,activity code Show, just a few 3 lines of important code to complete any complex logic

/**
* Login Interface
*
* @author Lipanquan
*/
public class Loginactivity extends Mvpbaseactivity {

@Override
protected void Settitle () {
GETAOP (). Settitle ();
}

@Override
Protected Activitypresenter Initaop () {
return new Loginactivitypresenter (this);
}

@Override
public boolean onKeyUp (int keycode, keyevent event) {
Return Getaop (). OnKeyUp (KeyCode, event);
}

}

2, Logic control code for login interface
/**
* Logical Control of Login interface
* Created by Lipanquan on 2016/12/7.
*/
public class Loginactivitypresenter extends Activitypresenter implements
Compoundbutton.oncheckedchangelistener, View.ontouchlistener, View.onfocuschangelistener, View.OnClickListener {

private static Final StringKey_is_remember_me = "Isrememberme";
private static Final StringKey_username = "USERNAME";
private static Final StringKey_password = "PWD";

@Override
public void Settitle () {
Settitle (r.string.Title_login);
Setleft (True, r.drawable.Selector_btn_back, r.string.String_back);
}

/**
* Login Interface Activity
*/
Private final mvpbaseactivity activity;

/**
* Construction Method
*
*@paramActivityMvpbaseactivity Object
*/
Public Loginactivitypresenter (mvpbaseactivity activity) {
This.activity = activity;
}

@Override
Protected Mvpbaseactivity getactivity () {
return activity;
}

/**
* User name Input box
*/
@ViewInject (r.id.Et_login_username)
Private EditText Et_login_username;

/**
* Password Entry box
*/
@ViewInject (r.id.Et_login_password)
Private EditText Et_login_password;

/**
* Remember the password
*/
@ViewInject (r.id.Cb_remember_me)
Private CheckBox Cb_remember_me;

/**
* Login button
*/
@ViewInject (r.id.Bt_login)
Private Button Bt_login;

/**
* This page
*/
@ViewInject (r.id.Ll_login_all)
Private View Ll_login_all;

/**
* Whether to remember the user
*/
Private Boolean isautologin = true;

/**
* User name icon
*/
@ViewInject (r.id.Iv_icon_password)
Private ImageView Iv_icon_password;

/**
* Quick Delete User name button
*/
@ViewInject (r.id.Iv_login_username_del)
Private ImageView Iv_login_username_del;

/**
* Password icon
*/
@ViewInject (r.id.Iv_icon_username)
Private ImageView Iv_icon_username;

/**
* Quick Delete Password button
*/
@ViewInject (r.id.Iv_login_pwd_del)
Private ImageView Iv_login_pwd_del;

/**
* Retrieve password
*/
@ViewInject (r.id.Tv_retrieve_password)
Private TextView Tv_retrieve_password;

/**
* Registration
*/
@ViewInject (r.id.Tv_register)
Private TextView Tv_register;
@Override public void OnCreate (Bundle savedinstancestate) {
Setcontentview (R.layout.activity_login);
Annotateutils.injectviews (this);
Inittitle (Getactivity ());
Initdate ();
Setlistener ();
}
/**
* Initialization
*/
private void Initdate () {
Setswipebackenable (TRUE);
This is the user name input box next focus is the password input box
Et_login_username.setnextfocusdownid (R.id.et_login_password);
Isautologin = Getboolean (Key_is_remember_me, true);
Cb_remember_me.setchecked (Isautologin);
Cb_remember_me.settextcolor (Getrescolor (
Cb_remember_me.ischecked ()? R.color.login_button_bg_color_normal
: R.color.six_six));
Et_login_username.settext (getString (key_username));
Et_login_password.settext (getString (Key_password));
Iv_icon_username.setenabled (FALSE);
Iv_icon_password.setenabled (FALSE);
if (!isnull (getString (key_username)))
Iv_icon_username.setenabled (TRUE);
if (!isnull (getString (Key_password)))
Iv_icon_password.setenabled (TRUE);
if (Et_login_password.gettext (). Length () > 0
&& et_login_username.gettext (). Length () > 0) {
Bt_login.setenabled (TRUE);
}
}
/**
* Set listener events for controls
*/
private void Setlistener () {
Cb_remember_me.setoncheckedchangelistener (this);
Bt_login.setonclicklistener (this);
Et_login_username.setonfocuschangelistener (this);
Et_login_password.setonfocuschangelistener (this);
Et_login_username.addtextchangedlistener (New Textwatcher () {
@Override
public void aftertextchanged (Editable s) {
if (s.tostring (). Length () > 0) {
if (s.tostring (). Length () > 10) {
if (! Utils.getinstance (). Ismobileno (S.tostring ())) {
Showtoastshort (R.string.toast_phone_error);
}
Iv_icon_username.setenabled (TRUE);
if (Et_login_password.gettext (). Length () > 5) {
Bt_login.setenabled (TRUE);
}
} else {
Bt_login.setenabled (FALSE);
Iv_icon_username.setenabled (FALSE);
}
Iv_login_username_del.setvisibility (view.visible);
} else {
Iv_login_username_del.setvisibility (View.gone);
Iv_icon_username.setenabled (FALSE);
}
}
});
Password entry Restrictions
Et_login_password.addtextchangedlistener (New Textwatcher () {
@Override
public void aftertextchanged (Editable s) {
if (s.tostring (). Length () > 0) {
if (s.tostring (). Length () > 5) {
if (Et_login_username.gettext (). toString (). Length () = = 11)
Bt_login.setenabled (TRUE);
Iv_icon_password.setenabled (TRUE);
} else {
Iv_icon_password.setenabled (FALSE);
}
Iv_login_pwd_del.setvisibility (view.visible);
} else {
Bt_login.setenabled (FALSE);
Iv_login_pwd_del.setvisibility (View.gone);
Iv_icon_password.setenabled (FALSE);
}
}
});
Iv_login_username_del.setonclicklistener (this);
Iv_login_pwd_del.setonclicklistener (this);
Ll_login_all.setontouchlistener (this);
Tv_retrieve_password.setonclicklistener (this);
Tv_register.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.tv_left://Return
Startexitactivity ();
Break
Case R.id.bt_login://Login
Loginuser (); Landing
Break
Case R.id.iv_login_username_del: {//Quick Delete user name
Et_login_username.settext ("");
Iv_login_username_del.setvisibility (View.gone);
Break
}
Case R.id.iv_login_pwd_del: {//Quick Delete password
Et_login_password.settext ("");
Iv_login_pwd_del.setvisibility (View.gone);
Break
}
Case R.id.tv_retrieve_password: {//Retrieve password
TODO: Jump to retrieve password interface
Break }
Case R.id.tv_register: {//Register
Startenteractivity (Registeractivity.class);
Break
}
}
}
/**
* Login
*/
private void Loginuser () {
if (! Netutils.isnetworkconnected (Getapplication ())) {
Showtoastshort (R.string.network_is_not_available_check_your_network_link);
Return
}
String username = Et_login_username.gettext (). toString (). Trim ();
String password = Et_login_password.gettext (). toString (). Trim ();
if (IsNull (username)) {
Showtoastshort (R.string.string_username_hint);
Return
}
if (! Utils.getinstance (). Ismobileno (username)) {
Showtoastshort (R.string.toast_phone_error);
Return
}
Bt_login.setenabled (FALSE);
TODO: Logon action
Startenteractivity (Mainactivity.class, true);
}
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
Switch (Buttonview.getid ()) {
Case R.ID.CB_REMEMBER_ME://Remember password ToggleButton status
Cb_remember_me.settextcolor (Getrescolor (
Cb_remember_me.ischecked ()? R.color.login_button_bg_color_normal
: R.color.six_six));
Putboolean (Key_is_remember_me, isChecked);
Isautologin = isChecked;
Break
}
}
@Override
public boolean OnTouch (View V, motionevent event) {
I (TAG, "OnTouch");
Closekeyboard ();
return false;
}
@Override
public void Onfocuschange (View V, Boolean hasfocus) {
Switch (V.getid ()) {
Case R.id.et_login_username://user name input box gets focus
if (Hasfocus && et_login_username.gettext (). toString (). Length () > 0) {
Iv_login_username_del.setvisibility (view.visible);
} else {
Iv_login_username_del.setvisibility (View.gone);
}
Break
Case R.id.et_login_password://Password input box gets focus
if (Hasfocus && et_login_password.gettext (). toString (). Length () > 0) {
Iv_login_pwd_del.setvisibility (view.visible);
} else {
Iv_login_pwd_del.setvisibility (View.gone);
}
Break
}
}
@Override
public boolean onKeyUp (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
Startexitactivity ();
return true;
}
Return Super.onkeyup (KeyCode, event);
}
}

3, Source address: Pending upload

Android MVP mode for component and business logic separation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.