首页

android 登陆实例源码下载

java

2020-7-30

/**
 * login.java
 * com.pk.cn
 *
 * Function: TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-11-2 		HP
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.pk.cn;

import com.pk.cn.net.UserDataServiceHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * ClassName:login Function: TODO ADD FUNCTION Reason: TODO ADD REASON
 * 
 * @author HP
 * @version
 * @since Ver 1.1
 * @Date 2012-11-2 上午10:31:27
 * @see
 */
public class Login extends Activity {
    private String userName;
    private String password;
    private String from;

    /** 以下是UI */
    private EditText view_userName;
    private EditText view_password;
    private CheckBox view_rememberMe;
    private Button view_loginSubmit;
    private Button view_loginRegister;
    private TextView view_fast;

    /** 用来操作sharePreferences标识 */
    private final String SHARE_LOGIN_TAG = "MAP_SHARE_LOGIN_TAG";

    /** 如果登录成功后,保存用户名到sharePreferences 以便下次不在输入了 */
    private String SHARE_LOGIN_USERNAME = "MAP_LOGIN_USERNAME";

    /** 如果邓丽成功,保存密码到sharePreferences 以便下次不在输入了 */
    private String SHARE_LOGIN_PASSWORD = "MAP_LOGIN_PASSWORD";

    /** 如果登录失败,这个可以给用户确切的消失提示 true 是网络连接失败,false是用户名和密码错误 */
    private boolean isNetError;

    /** 登录loading提示框 */
    private ProgressDialog proDialog;

    /** 条件成立 */
    boolean flag;

    private Context context;
    /** 声明 */
    UserDataServiceHelper UserDataService = new UserDataServiceHelper();

    /** 返回码 */

    // String Less = null;

    boolean StatusCode = false;

    /** 登录后台通知更新UI 主要是用户登录失败 通知UI更新界面 */
    Handler loginHandler = new Handler() {
        public void handleMessage(Message msg) {
            isNetError = msg.getData().getBoolean("isNetError");
            if (proDialog != null) {
                proDialog.dismiss();
            }
            if (isNetError) {
                Toast.makeText(Login.this, "登陆失败:\n1.请检查您网络连接!",
                        Toast.LENGTH_SHORT).show();

            } // 用户密码错误
            else {
                Toast.makeText(Login.this, "登陆失败,请输入正确的用户名和密码!",
                        Toast.LENGTH_SHORT).show();
                // 清除以前的sharePreferences
                clearSharePassword();
                clearShareName();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        findViewById();
        initView(false);
        // 需要去submitListener里面设置URL
        setListener();

    }

    /** 初始化注册VIEW组件 */
    private void findViewById() {
        view_userName = (EditText) findViewById(R.id.loginUserNameEdit);
        view_password = (EditText) findViewById(R.id.loginPasswordEdit);
        view_rememberMe = (CheckBox) findViewById(R.id.loginRememberMeCheckBox);
        view_loginSubmit = (Button) findViewById(R.id.loginSubmit);
        view_loginRegister = (Button) findViewById(R.id.loginRegister);
        view_fast = (TextView) findViewById(R.id.fast);

        /** 注册成功后传过来用户名和密码,显示在登录界面 */
        if (!flag) {
            Intent intent = getIntent();
            userName = intent.getStringExtra("name");
            password = intent.getStringExtra("pw");
            from = intent.getStringExtra("fm");
            view_rememberMe.setChecked(false);//小BUG
            view_userName.setText(userName);
            view_password.setText(password);
        }

    }

    /**
     * initView:(初始化界面)
     * 
     * @param isRememberMe如果当时点击了RememberMe
     *            ,并且登陆成功过一次,则saveSharePreferences(true,ture)后,则直接进入
     * @return void DOM对象
     * @throws
     * @since CodingExample Ver 1.1
     */
    private void initView(boolean isRememberMe) {
        SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
        String userName = share.getString(SHARE_LOGIN_USERNAME, "");
        String password = share.getString(SHARE_LOGIN_PASSWORD, "");
        Log.i(this.toString(), "userName="   userName   " password="   password);
        if (!"".equals(userName)) {
            view_userName.setText(userName);
        }
        if (!"".equals(password)) {
            view_password.setText(password);
            view_rememberMe.setChecked(true);
        }
        // 如果密码也保存了 直接让登录按钮获取焦点
        if (view_password.getText().toString().length() > 0) {
            // view_loginSubmit.requestFocus();
            // view_password.requestFocus();
        }
        share = null;
    }

    /** 设置一个监听器 */
    private void setListener() {
        view_loginSubmit.setOnClickListener(submitListener);
        view_loginRegister.setOnClickListener(registerLstener);
        view_rememberMe.setOnCheckedChangeListener(rememberMeListener);
        view_fast.setOnClickListener(fastLstener);

    }

    /** 记住我的选项是否勾选 */
    private boolean isRememberMe() {
        if (view_rememberMe.isChecked()) {
            return true;
        }
        return false;
    }

    /** 登录Button Listener */
    private OnClickListener submitListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("TAG", "submitListener");
            proDialog = ProgressDialog.show(Login.this, "连接中..",
                    "连接中..请稍后....", true, true);
            // 开启一个线程进行登录验证,主要是用户失败成功可以直接通过startAcitivity(Intent)转向
            Thread loginThread = new Thread(new LoginFailureHandler());
            loginThread.start();// 开启

        }
    };

    /** 注册Listener */
    private OnClickListener registerLstener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(Login.this, Register.class);
            // 转向注册页面
            startActivity(intent);
        }
    };

    /** 记住我checkBoxListener */
    private OnCheckedChangeListener rememberMeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (view_rememberMe.isChecked()) {
                Toast.makeText(Login.this, "如果登录成功,以后账号和密码会自动输入!",
                        Toast.LENGTH_SHORT).show();
            }
        }
    };

    /** 快速注册TextView Listener */
    private OnClickListener fastLstener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            /** 请求服务器 */
            UserDataServiceHelper.fast(context, "quickreg", from);
            /** 得到返回的注册名字 */
            String FastUserName = UserDataService.FastUserName;
            /** 得到返回的密码 */
            String FastPassWord = UserDataService.FastPassWord;
            /** 得到返回的UID */
            String UerId = UserDataService.Uid;
            /** 执行跳转 通过bundle putString传值到另一个页面 */
            Intent intent = new Intent();
            intent.setClass(Login.this, IndexPage.class);
            Bundle bundle = new Bundle();
            bundle.putString("LOGIN_USERNAME", FastUserName);
            bundle.putString("LOGIN_PASSWORD", FastPassWord);
            bundle.putString("LOGIN_ID", UerId);
            intent.putExtras(bundle);
            // 转向登陆后的页面
            startActivity(intent);

            // TODO Auto-generated method stub

        }

    };

    // 另起一个线程登录
    class LoginFailureHandler implements Runnable {
        @Override
        public void run() {
            userName = view_userName.getText().toString();
            Log.i("TAG", "userName LoginFailureHandler"   userName);
            password = view_password.getText().toString();
            Log.i("TAG", "password LoginFailureHandler"   password);

            /** 请求服务器 */
            if (userName != null || password != null) {
                boolean loginState = UserDataServiceHelper.logins(context, "login", userName,
                        password, from);

                Log.i("TAG", "登录返回条件"   loginState);
                // 登录成功
                if (loginState) {
                    String LoginUerId = UserDataService.LoginUid;
                    // 需要传输数据到登陆后的界面,
                    Intent intent = new Intent();
                    intent.setClass(Login.this, IndexPage.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("LOGIN_USERNAME", userName);
                    bundle.putString("LOGIN_PASSWORD", password);
                    bundle.putString("LOGIN_ID", LoginUerId);
                    intent.putExtras(bundle);
                    // 转向登陆后的页面
                    startActivity(intent);
                    // /** 得到请求服务器返回码 */
                    String loginStateInt = UserDataService.results;
                    int Less = Integer.valueOf(loginStateInt); // 转换成整形
                    Log.i("TAG", "登录后的返回码:"   Less);
                    if (Less == 1) {
                        StatusCode = true;
                        
                    }

                    // 登录成功记住帐号密码
                    if (StatusCode) {
                        if (isRememberMe()) {
                            saveSharePreferences(true, true);
                        } else {
                            saveSharePreferences(true, false);
                        }

                    } else {
                        // 如果不是网络错误
                        if (!isNetError) {
                            clearSharePassword();
                            clearShareName();

                        }

                    }
                    if (!view_rememberMe.isChecked()) {
                        clearSharePassword();
                        clearShareName();
                    }
                    proDialog.dismiss();
                } else {
                    // 通过调用handler来通知UI主线程更新UI,
                    Log.i("TAG", "连接失败");
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isNetError", isNetError);
                    message.setData(bundle);
                    loginHandler.sendMessage(message);
                }

            }

        }
    }

    /**
     * 如果登录成功过,则将登陆用户名和密码记录在SharePreferences
     * 
     * @param saveUserName 是否将用户名保存到SharePreferences
     * @param savePassword 是否将密码保存到SharePreferences
     */
    private void saveSharePreferences(boolean saveUserName, boolean savePassword) {
        SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
        if (saveUserName) {
            Log.d(this.toString(), "saveUserName="
                      view_userName.getText().toString());
            share.edit().putString(SHARE_LOGIN_USERNAME,
                    view_userName.getText().toString()).commit();
        }
        if (savePassword) {
            share.edit().putString(SHARE_LOGIN_PASSWORD,
                    view_password.getText().toString()).commit();
        }
        share = null;
    }

    /** 清除密码 */
    private void clearSharePassword() {
        SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
        share.edit().putString(SHARE_LOGIN_PASSWORD, "").commit();
        share = null;

    }

    /** 清除用户信息 */
    private void clearShareName() {
        SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
        share.edit().putString(SHARE_LOGIN_USERNAME, "").commit();
        share = null;

    }

}

    

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
andoird96pk/.classpath , 348
andoird96pk/.project , 847
andoird96pk/AndroidManifest.xml , 1561
andoird96pk/bin/classes.dex , 42816
andoird96pk/bin/com/pk/cn/AddData$1.class , 1190
andoird96pk/bin/com/pk/cn/AddData$LoginAddHandler.class , 2832
andoird96pk/bin/com/pk/cn/AddData.class , 3005
andoird96pk/bin/com/pk/cn/AddPay$1.class , 967
andoird96pk/bin/com/pk/cn/AddPay$2.class , 967
andoird96pk/bin/com/pk/cn/AddPay$3.class , 967
andoird96pk/bin/com/pk/cn/AddPay$4.class , 1181
andoird96pk/bin/com/pk/cn/AddPay$AddPayHandler.class , 2901
andoird96pk/bin/com/pk/cn/AddPay.class , 4029
andoird96pk/bin/com/pk/cn/Andoird96pkActivity.class , 539
andoird96pk/bin/com/pk/cn/IndexPage$1.class , 695
andoird96pk/bin/com/pk/cn/IndexPage$2.class , 1378
andoird96pk/bin/com/pk/cn/IndexPage$3.class , 1377
andoird96pk/bin/com/pk/cn/IndexPage$4.class , 1268
andoird96pk/bin/com/pk/cn/IndexPage$5.class , 1252
andoird96pk/bin/com/pk/cn/IndexPage.class , 2931
andoird96pk/bin/com/pk/cn/Login$1.class , 1387
andoird96pk/bin/com/pk/cn/Login$2.class , 1301
andoird96pk/bin/com/pk/cn/Login$3.class , 931
andoird96pk/bin/com/pk/cn/Login$4.class , 1137
andoird96pk/bin/com/pk/cn/Login$5.class , 1696
andoird96pk/bin/com/pk/cn/Login$LoginFailureHandler.class , 3904
andoird96pk/bin/com/pk/cn/Login.class , 7111
andoird96pk/bin/com/pk/cn/R$attr.class , 313
andoird96pk/bin/com/pk/cn/R$drawable.class , 443
andoird96pk/bin/com/pk/cn/R$id.class , 1981
andoird96pk/bin/com/pk/cn/R$layout.class , 665
andoird96pk/bin/com/pk/cn/R$string.class , 1670
andoird96pk/bin/com/pk/cn/R.class , 468
andoird96pk/bin/com/pk/cn/Register$1.class , 1140
andoird96pk/bin/com/pk/cn/Register$2.class , 691
andoird96pk/bin/com/pk/cn/Register.class , 4371
andoird96pk/bin/com/pk/cn/RestorePassWord$1.class , 1623
andoird96pk/bin/com/pk/cn/RestorePassWord.class , 2226
andoird96pk/bin/com/pk/cn/SumPay.class , 481
andoird96pk/bin/com/pk/cn/SumUp.class , 1872
andoird96pk/bin/com/pk/cn/WeDialog$1.class , 659
andoird96pk/bin/com/pk/cn/WeDialog$2.class , 659
andoird96pk/bin/com/pk/cn/WeDialog$3.class , 1212
andoird96pk/bin/com/pk/cn/WeDialog$LoginFailureHandler.class , 2342
andoird96pk/bin/com/pk/cn/WeDialog.class , 2887
andoird96pk/bin/resources.ap_ , 40850
andoird96pk/default.properties , 362
andoird96pk/gen/com/pk/cn/R.java , 6056
andoird96pk/proguard.cfg , 1248
andoird96pk/res/drawable-hdpi/icon.png , 4147
andoird96pk/res/drawable-hdpi/ic_personal_avatar.png , 4331
andoird96pk/res/drawable-hdpi/logo.png , 12637
andoird96pk/res/drawable-ldpi/icon.png , 1723
andoird96pk/res/drawable-mdpi/icon.png , 2574
andoird96pk/res/layout/accounts.xml , 4284
andoird96pk/res/layout/adddata_dialog.xml , 3551
andoird96pk/res/layout/addpays.xml , 4780
andoird96pk/res/layout/custom_dialog.xml , 3348
andoird96pk/res/layout/indexpage.xml , 2072
andoird96pk/res/layout/login.xml , 1920
andoird96pk/res/layout/main.xml , 382
andoird96pk/res/layout/register.xml , 1652
andoird96pk/res/layout/restorepword.xml , 1431
andoird96pk/res/layout/sumpay.xml , 1120
andoird96pk/res/values/indexpage.xml , 318
andoird96pk/res/values/login.xml , 593
andoird96pk/res/values/register.xml , 1212
andoird96pk/res/values/strings.xml , 181
andoird96pk/src/com/pk/cn/AddData.java , 4873
andoird96pk/src/com/pk/cn/AddPay.java , 7317
andoird96pk/src/com/pk/cn/Andoird96pkActivity.java , 571
andoird96pk/src/com/pk/cn/IndexPage.java , 5274
andoird96pk/src/com/pk/cn/Login.java , 13505
andoird96pk/src/com/pk/cn/Register.java , 4695
andoird96pk/src/com/pk/cn/RestorePassWord.java , 2712
andoird96pk/src/com/pk/cn/SumPay.java , 842
andoird96pk/src/com/pk/cn/SumUp.java , 2811
andoird96pk/src/com/pk/cn/WeDialog.java , 5337
没有账号? 忘记密码?

社交账号快速登录