完成登陆实例

系统 2078 0

 

 

 
完成登陆实例
 

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loginRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white1"
    android:orientation="vertical" >

    <include layout="@layout/header_account_bar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/login_chart"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:orientation="horizontal" >

                <TextView
                    style="@style/account_register_textview"
                    android:text="@string/account_name" />

                <EditText
                    android:id="@+id/name_text"
                    style="@style/account_register_edittext" />
            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/login_chart_line" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:orientation="horizontal" >

                <TextView
                    style="@style/account_register_textview"
                    android:text="@string/account_pwd" />

                <EditText
                    android:id="@+id/password_text"
                    style="@style/account_register_edittext"
                    android:password="true" />
            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/login_chart_line" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <TextView
                    style="@style/account_register_textview"
                    android:layout_gravity="center_vertical"
                    android:text="@string/register_account_type" />

                <RelativeLayout style="@style/account_register_edittext" >

                    <EditText
                        android:id="@+id/type_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:editable="false"
                        android:singleLine="true"
                        android:text="@string/register_account_type_default"
                        android:textColor="@color/textcolor" />

                    <ImageButton
                        android:id="@+id/type_button"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_alignBottom="@+id/type_text"
                        android:layout_alignRight="@+id/type_text"
                        android:layout_alignTop="@+id/type_text"
                        android:layout_marginBottom="2dp"
                        android:layout_marginRight="1dp"
                        android:layout_marginTop="1dp"
                        android:background="@drawable/register_more_select" />
                </RelativeLayout>
            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/login_chart_line"
                android:visibility="gone" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <CheckBox
                    android:id="@+id/login_cb_savepwd"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:button="@drawable/cpse_btn_check"
                    android:checked="true"
                    android:text="@string/save_pwd"
                    android:textColor="@color/textcolor"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" >

                <Button
                    android:id="@+id/login_btn_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:background="@drawable/new_account"
                    android:text="@string/account_register"
                    android:textColor="@color/textcolor"
                    android:textSize="18sp" />
                <!--
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/login_btn_register"
                    android:layout_alignRight="@+id/login_btn_register"
                    android:layout_alignTop="@+id/login_btn_register"
                    android:layout_marginBottom="2dp"
                    android:layout_marginRight="1dp"
                    android:layout_marginTop="1dp"
                    android:background="@drawable/regist_goto" />
                -->
            </RelativeLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/bg_line" />

            <Button
                android:id="@+id/login_button"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/register_finish_button"
                android:text="@string/login"
                android:textColor="@color/white1"
                android:textSize="22sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

 

 

 

 

AccountItem.java

 

 



import java.io.Serializable;

public class AccountItem implements Serializable {

    private static final long serialVersionUID = 1L;
   
    private int id;
    private String username;
    private String email;
    private String type;
    private String truename;
    private String password;
    private String repwd;
    private String country;
    private String company;
    private String job;
    private String productRange;
    private String department;
    private String mobile;
   
    private String qc_url;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getTruename() {
        return truename;
    }
    public void setTruename(String truename) {
        this.truename = truename;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getRepwd() {
        return repwd;
    }
    public void setRepwd(String repwd) {
        this.repwd = repwd;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getProductRange() {
        return productRange;
    }
    public void setProductRange(String productRange) {
        this.productRange = productRange;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
   
    public String getQc_url() {
        return qc_url;
    }
    public void setQc_url(String qc_url) {
        this.qc_url = qc_url;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    @Override
    public String toString() {
        String tmp = "Account : id = "+id+";username = "+username +";company = "+company;
        return tmp;
    }

}

 

 

 

 

 

 

AccountLoginActivity.java

 

 

 

package com.cps.media.ui.account;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cps.media.R;
import com.cps.media.model.AccountItem;
import com.cps.media.model.ExhibitorItem;
import com.cps.media.ui.LoadingActivity;
import com.cps.media.ui.exhibitors.ExhibitorActivity;
import com.cps.media.util.CONST;
import com.cps.media.util.Util;

public class AccountLoginActivity extends Activity implements
        View.OnClickListener, OnTouchListener {

    private String TAG = "AccountLoginActivity";

    private ImageButton mBackImageButton;

    private Button mRegisterButton;

    private EditText mTypeEditText;

    private EditText mNameEditText;

    private EditText mPwdEditText;

    private ImageButton mTypeSelectButton;

    private AccountItem mAccount;

    private Button mLoginButton;
   
    private SharedPreferences mConfig;
   
    private CheckBox mSaveAccountCheckBox;
   
    private Intent mIntent;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_account_login);

        mConfig = this.getSharedPreferences(Util.CONFIG_FILE, MODE_PRIVATE);

        mAccount = new AccountItem();
       
        //set default account type
        String[] values = getResources().getStringArray(
                R.array.account_type_value);
        mAccount.setType(values[0]);
       
        mIntent = new Intent(this,LoadingActivity.class);

        mSaveAccountCheckBox = (CheckBox) findViewById(R.id.login_cb_savepwd);
        mSaveAccountCheckBox.setOnClickListener(this);
        mBackImageButton = (ImageButton) findViewById(R.id.btn_newscontent_back);
        mBackImageButton.setOnClickListener(this);
        mRegisterButton = (Button) findViewById(R.id.login_btn_register);
        mRegisterButton.setOnClickListener(this);
        mTypeEditText = (EditText) findViewById(R.id.type_text);
        mTypeEditText.setOnClickListener(this);
        mTypeEditText.setOnTouchListener(this);
        mTypeSelectButton = (ImageButton) findViewById(R.id.type_button);
        mTypeSelectButton.setOnClickListener(this);
        mNameEditText = (EditText) findViewById(R.id.name_text);
        mPwdEditText = (EditText) findViewById(R.id.password_text);
        mLoginButton = (Button) findViewById(R.id.login_button);
        mLoginButton.setOnClickListener(this);
    }
   
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
       
        //如果上次有记住账号密码,则在此处赋值显示出来
        String[] values = getResources().getStringArray(
                R.array.account_type_value);
        if(mConfig.contains("issave")){
            if(mConfig.getBoolean("issave", false)){
                mAccount.setId(mConfig.getInt("id", 0));
                mAccount.setUsername(mConfig.getString("name", ""));
                mAccount.setPassword(mConfig.getString("pwd", ""));
                mAccount.setEmail(mConfig.getString("email", ""));
                mAccount.setType(mConfig.getString("type", values[0]));
                mNameEditText.setText(mAccount.getUsername());
                mPwdEditText.setText(mAccount.getPassword());
                mSaveAccountCheckBox.setChecked(true);
                String[] types = getResources().getStringArray(
                        R.array.account_type); // array
               
                int tmp = 0;
                for (int j = 0; j < values.length; j++) {
                    if (values[j].toString().trim().equals(mConfig.getString("type", values[0]))) {
                        tmp = j;
                        break;
                    }
                }
               
                mTypeEditText.setText(types[tmp]);
               
            }else{
                    mSaveAccountCheckBox.setChecked(false);
            }
        }else{
            mSaveAccountCheckBox.setChecked(true);
            SharedPreferences.Editor editor = mConfig.edit();
            editor.putBoolean("issave", mSaveAccountCheckBox.isChecked());
            editor.commit();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.btn_newscontent_back:
            backKey();
            break;
        case R.id.login_btn_register:
            Intent intent = new Intent(this, AccountRegisterActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.news_dync_in_from_right, R.anim.news_dync_out_to_left);
            break;
        case R.id.type_button:
            showChooseTypeDialog();
            break;
        case R.id.login_button:
            String name = mNameEditText.getText().toString().trim();
            String pwd = mPwdEditText.getText().toString().trim();
            if (name == null || name.equals("") || pwd == null
                    || pwd.equals("")) {
                Toast.makeText(AccountLoginActivity.this, getResources().getString(R.string.notify_name_pwd), Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            mLoginButton.setEnabled(false);
            mAccount.setUsername(mNameEditText.getText().toString().trim());
            mAccount.setPassword(mPwdEditText.getText().toString().trim());
            submitData();
            break;
        case R.id.login_cb_savepwd:
            SharedPreferences.Editor editor = mConfig.edit();
            if(!mSaveAccountCheckBox.isChecked()){
                editor.clear();
            }
            editor.putBoolean("issave", mSaveAccountCheckBox.isChecked());
            editor.commit();
            break;
        default:
            break;
        }
    }
   
    private void submitData() {
       
        mIntent.putExtra(Util.ACCOUNT_TYPE, Util.ACCOUNT_LOGIN_VALUE);
        startActivity(mIntent);
        Thread t = new Thread() {
            @Override
            public void run() {
                super.run();
                HttpClient client = new DefaultHttpClient();
                StringBuilder builder = new StringBuilder();
                int error = 1;
                String url = CONST.URL_ACCOUNT_LOGIN;
                try {
                    url = url.replace("{0}", URLEncoder.encode(mAccount.getUsername(),CONST.URL_ENCODING_UTF_8));
                    url = url.replace("{1}", URLEncoder.encode(mAccount.getPassword(),CONST.URL_ENCODING_UTF_8));
                    url = url.replace("{2}", URLEncoder.encode(mAccount.getType(),CONST.URL_ENCODING_UTF_8));
                } catch (Exception e) {
                    e.printStackTrace();
                }
               
                Log.d(TAG,"url = "+ url);
                HttpGet myget = new HttpGet(url);

                try {
                    HttpResponse response = client.execute(myget);
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent()));
                    for (String s = reader.readLine(); s != null; s = reader
                            .readLine()) {
                        builder.append(s);
                    }
                    JSONObject jsonObject = new JSONObject(builder.toString());
                    error = jsonObject.getInt("errno");
                    String errormsg = jsonObject.getString("errmsg");
                    Log.v("wgp", "error=" + error);
                    Log.v("wgp", "errormsg=" + errormsg);
                    if (error == 0) {
                        mAccount.setId(jsonObject.getJSONObject("data").getInt("userid"));
                        mAccount.setEmail(jsonObject.getJSONObject("data").getString("email"));
                        try {
                            mAccount.setQc_url(jsonObject.getJSONObject("data").getString("qc_url"));
                        } catch (Exception e) {
                            Log.d(TAG,"user have no qc_url");
                        }
                        handler.sendEmptyMessage(1);
                    }else{
                        handler.sendEmptyMessage(-1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(-1);
                } finally {
                }
            }
        };
        t.start();
    }
   
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Intent intent = new Intent(Util.LOADING_BROADCAST_RECIVER);
            sendBroadcast(intent);
            if(msg.what == 1){
                SharedPreferences.Editor editor = mConfig.edit();
                Log.d(TAG, mAccount.toString());
                if(mSaveAccountCheckBox.isChecked()){
                    //写入需要记住的信息
                    editor.putInt("id", mAccount.getId());
                    editor.putString("name", mAccount.getUsername());
                    editor.putString("pwd", mAccount.getPassword());
                    editor.putString("email", mAccount.getEmail());
                    editor.putString("type", mAccount.getType());
                    editor.putString("qc_url", mAccount.getQc_url());
                    editor.commit();
                }
                AccountManager.getInstance().setAccount(mAccount);
                Toast.makeText(AccountLoginActivity.this, getResources().getString(R.string.welcome), Toast.LENGTH_SHORT).show();

                goAccountCenter();
            }else if(msg.what == -1){
                Toast.makeText(AccountLoginActivity.this, getResources().getString(R.string.login_failure), Toast.LENGTH_SHORT).show();
            }
            mLoginButton.setEnabled(true);
//            sendBroadcast(mIntent);
        };
    };
   
    private void goAccountCenter(){
        if(mAccount.getType().equals(AccountManager.TYPE_USER)){
            ExhibitorItem item = new ExhibitorItem();
            item.setId(mAccount.getId());
            Intent intent = new Intent(this,ExhibitorActivity.class);
            intent.putExtra("exhibitor_item",item);
            intent.putExtra("scan", true);
            startActivity(intent);
            finish();
            //切换时的动画
            overridePendingTransition(R.anim.news_dync_in_from_right,R.anim.news_dync_out_to_left);
        }else if(mAccount.getType().equals(AccountManager.TYPE_VIEWER)){
            Intent intent = new Intent(AccountLoginActivity.this,AccountCenterActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.news_dync_in_from_right,R.anim.news_dync_out_to_left);
        }
    }
   
    private void backKey() {
        setResult(RESULT_OK);
        finish();
        overridePendingTransition(R.anim.news_dync_in_from_left,R.anim.news_dync_out_to_right);
    }

    private void showChooseTypeDialog() {
        final String[] types = getResources().getStringArray(
                R.array.account_type); // array
        final String[] values = getResources().getStringArray(
                R.array.account_type_value);
        String defualt = mTypeEditText.getText().toString();
        int tmp = 0;
        for (int j = 0; j < types.length; j++) {
            if (types[j].toString().trim().equals(defualt)) {
                tmp = j;
                break;
            }
        }
       
        //弹出对话框选择类型
        new AlertDialog.Builder(AccountLoginActivity.this)
                .setTitle("请选择用户类别")
                //单选
                .setSingleChoiceItems(types, tmp,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                mTypeEditText.setText(types[which]);
                                mAccount.setType(values[which]);
                                dialog.dismiss();
                            }

                        }).show();
    }

    //xml类型选择的touch事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            int id = v.getId();
            switch (id) {
            case R.id.type_text:
                showChooseTypeDialog();
                break;
            default:
                break;
            }
        }
        return false;
    }
}

 

 

 

 

 

 

完成登陆实例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论