1,Activity代码展示,只需要一下3行重要代码即可完成任何复杂的逻辑
/** * 登录界面 * * @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,登录界面的逻辑控制代码/** * 登录界面的逻辑控制 * Created by lipanquan on 2016/12/7. */public class LoginActivityPresenter extends ActivityPresenter implements CompoundButton.OnCheckedChangeListener, View.OnTouchListener, View.OnFocusChangeListener, View.OnClickListener { private static final String KEY_IS_REMEMBER_ME = "isRememberMe"; private static final String KEY_USERNAME = "userName"; private static final String KEY_PASSWORD = "pwd"; @Override public void setTitle() { setTitle(R.string.title_login); setLeft(true, R.drawable.selector_btn_back, R.string.string_back); } /** * 登录界面activity */ private final MVPBaseActivity activity; /** * 构造方法 * * @param activity MVPBaseActivity对象 */ public LoginActivityPresenter(MVPBaseActivity activity) { this.activity = activity; } @Override protected MVPBaseActivity getActivity() { return activity; } /** * 用户名输入框 */ @ViewInject(R.id.et_login_username) private EditText et_login_username; /** * 密码输入框 */ @ViewInject(R.id.et_login_password) private EditText et_login_password; /** * 记住密码 */ @ViewInject(R.id.cb_remember_me) private CheckBox cb_remember_me; /** * 登陆按钮 */ @ViewInject(R.id.bt_login) private Button bt_login; /** * 这个页面 */ @ViewInject(R.id.ll_login_all) private View ll_login_all; /** * 是否记住用户 */ private boolean isAutoLogin = true; /** * 用户名icon */ @ViewInject(R.id.iv_icon_password) private ImageView iv_icon_password; /** * 快速删除用户名按钮 */ @ViewInject(R.id.iv_login_username_del) private ImageView iv_login_username_del; /** * 密码icon */ @ViewInject(R.id.iv_icon_username) private ImageView iv_icon_username; /** * 快速删除密码按钮 */ @ViewInject(R.id.iv_login_pwd_del) private ImageView iv_login_pwd_del; /** * 找回密码 */ @ViewInject(R.id.tv_retrieve_password) private TextView tv_retrieve_password; /** * 注册 */ @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(); } /** * 初始化 */ private void initDate() { setSwipeBackEnable(true); // 这是用户名输入框 下一个焦点为密码输入框 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); } } /** * 设置控件的 监听事件 */ 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); } } }); // 密码输入限制 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: // 返回 startExitActivity(); break; case R.id.bt_login: // 登录 loginUser(); // 登陆 break; case R.id.iv_login_username_del: { // 快速删除用户名 et_login_username.setText(""); iv_login_username_del.setVisibility(View.GONE); break; } case R.id.iv_login_pwd_del: { // 快速删除密码 et_login_password.setText(""); iv_login_pwd_del.setVisibility(View.GONE); break; } case R.id.tv_retrieve_password: { // 找回密码 // TODO:跳转到找回密码界面 break; } case R.id.tv_register: { // 注册 // startEnterActivity(RegisterActivity.class); break; } } } /** * 登陆 */ 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: 登录操作 startEnterActivity(MainActivity.class, true); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.cb_remember_me: // 记住密码 ToggleButton状态 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: // 用户名输入框获得焦点 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: // 密码输入框获得焦点 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,源码地址:待上传
时间: 2024-10-06 04:17:06