android自动登录的实现

来源:互联网 发布:淘宝开店本人营业执照 编辑:程序博客网 时间:2024/05/22 11:20

自动登陆的界面
1,进入界面,从SharedPreferences中获得 记住密码的状态ischeck和自动登录isAuto。默认返回0
2,如果自动登录(isAuto==1),则启动线程,隐藏登录界面,显示自动登录,直接跳到第二个界面
3,如果记住了密码(ischeck==1),就在从SharedPreference中获取密码和账号并设置给控件,并设置复选框为选中状态。
4,添加自动登录和记住密码的监听器:若选择自动登录则记住密码也选中,
若在两者都选中的情况下取消记住密码则都取消
5,添加登录按钮监听器:判断账号密码不为空,在判断密码账号正确性:如果都成立,
//判断
(1)自动登录被选中:设置ischeck和isAuto为1,并记住密码
(2)只有记住密码被选中时,设置ischeck为1,并记住密码
(3)都未选中时,设置ischeck和isAuto为0。
跳转界面
否则,提示错误信息。
这里写图片描述
这里写图片描述
这里写图片描述
登陆代码:

package bzu.hou.login;import android.opengl.Visibility;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class LoginActivity extends Activity {    /*        编程思路自然语言描述::        1,进入界面,从SharedPreferences中获得 记住密码的状态ischeck和自动登录isAuto。默认返回0        2,如果自动登录(isAuto==1),则启动线程,隐藏登录界面,显示自动登录,直接跳到第二个界面        3,如果记住了密码(ischeck==1),就在从SharedPreference中获取密码和账号并设置给控件,并设置复选框为选中状态。        4,添加自动登录和记住密码的监听器:若选择自动登录则记住密码也选中,若在两者都选中的情况下取消记住密码则都取消        5,添加登录按钮监听器:判断账号密码不为空,在判断密码账号正确性:如果都成立,                            //判断                            (1)自动登录被选中:设置ischeck和isAuto为1,并记住密码                            (2)只有记住密码被选中时,设置ischeck为1,并记住密码                            (3)都未选中时,设置ischeck和isAuto为0。                                跳转界面                            否则,提示错误信息。     *     */    private EditText user,pass =null;    private CheckBox checkpass,auto_login =null;    private Button login =null;    private LinearLayout main =null;    private TextView auto =null;    private String str_user,str_pass =null;    private SharedPreferences shared =null;    private SharedPreferences.Editor editor =null;    private int isCheck =0; //是否记住密码 0默认 不记住    private int isAuto =0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        InitView();        //        shared =this.getSharedPreferences("login", Context.MODE_PRIVATE);        editor =shared.edit();        isAuto =shared.getInt("auto", 0);        isCheck =shared.getInt("check",0); //刚进入界面获取 是否记住密码的状态        //自动登录        if(isAuto ==1){            new Thread(new Runnable() {                @Override                public void run() {                    // TODO Auto-generated method stub                    try {                        main.setVisibility(View.GONE); //隐藏登录界面                        auto.setVisibility(View.VISIBLE);//显示自动登录                        Thread.sleep(2000);                        Intent intent =new Intent(LoginActivity.this,TwoActivity.class);                        startActivity(intent);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }).start();        }        //记住密码        if(isCheck==1){            //记住密码,从SharedPreferences中就获取账号密码            str_user =shared.getString("user", "");            str_pass =shared.getString("pass", "");            //设置给控件            user.setText(str_user);            pass.setText(str_pass);            //设置控件为选中状态            checkpass.setChecked(true);        }else {            //不记住密码            checkpass.setChecked(false);        }        //自动登录监听器        auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton arg0, boolean bool) {                // TODO Auto-generated method stub                if(bool){//自动登录选中,记住密码也选中。                    checkpass.setChecked(true);                }            }        });        //记住密码监听器        checkpass.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton arg0, boolean bool) {                // TODO Auto-generated method stub                if(!bool){                    auto_login.setChecked(false);                    //防止 在正常登录后,在次进入登录界面时,只做了取消记住密码操作,而没有登录的情况。                    editor.putInt("check",0);                    editor.putInt("auto", 0);                    editor.commit();                }            }        });        //登录事件        login.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                if(isLogin()){//判断不为空                    if(str_user.equals("123") && str_pass.equals("123")){//判断账号密码正确                        //在登录时,判断控件是否记住密码,只有正确登录才能保存密码                        if(auto_login.isChecked()){                            editor.putInt("check",1);                            editor.putInt("auto", 1);                            editor.putString("user", str_user);                            editor.putString("pass",str_pass);                        }else if(checkpass.isChecked()){                            editor.putInt("check",1);                            editor.putString("user", str_user);                            editor.putString("pass",str_pass);                        }else {                            editor.putInt("check",0);                            editor.putInt("auto", 0);                        }                        editor.commit();                        Intent intent =new Intent(LoginActivity.this,TwoActivity.class);                        startActivity(intent);                    }else {                        Toast.makeText(LoginActivity.this, "账号或密码错误!", 3000).show();                    }                }else {                    Toast.makeText(LoginActivity.this, "账号或密码不能为空!", 3000).show();                }            }        });    }    private Boolean isLogin(){        str_user =user.getText().toString();        str_pass =pass.getText().toString();        if(str_user.equals("") || str_pass.equals(""))            return false;        return true;    }    private void InitView() {        // TODO Auto-generated method stub        user =(EditText)findViewById(R.id.user);        pass =(EditText)findViewById(R.id.pass);        login =(Button)findViewById(R.id.login);        checkpass =(CheckBox)findViewById(R.id.checkpass);        auto_login =(CheckBox)findViewById(R.id.auto_login);        auto =(TextView)findViewById(R.id.auto);        main =(LinearLayout)findViewById(R.id.mian);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.login, menu);        return true;    }//在登录后,按下返回键    @Override    protected void onRestart() {        // TODO Auto-generated method stub        super.onRestart();        isAuto =shared.getInt("auto", 0);        main.setVisibility(View.VISIBLE);        auto.setVisibility(View.GONE);        if(isAuto==1){ //获得自动登录的状态,在界面二可能会改变            auto_login.setChecked(true);        }else {            auto_login.setChecked(false);        }    }}

部分布局

<span style="font-family:'Courier New';"><?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:background="@drawable/logo_bg"      android:orientation="vertical" >      <RelativeLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content" >          <ImageButton               android:id="@+id/img_btn"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentRight="true"              android:background="@drawable/quit"/>          <TextView              android:id="@+id/tv_zh"              android:layout_width="wrap_content"              android:layout_height="35dip"              android:layout_marginLeft="12dip"              android:layout_marginTop="10dip"              android:gravity="bottom"              android:text="帐号:"              android:textColor="#000000"              android:textSize="18sp" />          <EditText              android:id="@+id/et_zh"              android:layout_width="fill_parent"              android:layout_height="40dip"              android:layout_below="@id/tv_zh"              android:layout_marginLeft="12dip"              android:layout_marginRight="10dip" />          <TextView              android:id="@+id/tv_mima"              android:layout_width="wrap_content"              android:layout_height="35dip"              android:layout_below="@id/et_zh"              android:layout_marginLeft="12dip"              android:layout_marginTop="10dip"              android:gravity="bottom"              android:text="密码:"              android:textColor="#000000"              android:textSize="18sp" />          <EditText              android:id="@+id/et_mima"              android:layout_width="fill_parent"              android:layout_height="40dip"              android:layout_below="@id/tv_mima"              android:layout_marginLeft="12dip"              android:layout_marginRight="10dip"              android:maxLines="200"              android:password="true"              android:scrollHorizontally="true" />          <CheckBox              android:id="@+id/cb_mima"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_below="@id/et_mima"              android:layout_marginLeft="12dip"              android:text="记住密码"              android:textColor="#000000" />          <CheckBox              android:id="@+id/cb_auto"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_below="@id/cb_mima"              android:layout_marginLeft="12dip"              android:text="自动登录"              android:textColor="#000000" />          <Button              android:id="@+id/btn_login"              android:layout_width="80dip"              android:layout_height="40dip"              android:layout_below="@id/et_mima"              android:layout_alignParentRight="true"              android:layout_alignTop="@id/cb_auto"              android:layout_marginRight="10dip"              android:gravity="center"              android:text="登录"              android:textColor="#000000"              android:textSize="18sp"/>      </RelativeLayout>  </LinearLayout></span>  logo.xml文件[html] view plaincopy<span style="font-family:'Courier New';"><?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:background="@drawable/logo_bg"      android:orientation="vertical" >      <RelativeLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"           android:layout_weight="3">          <ProgressBar              android:id="@+id/pgBar"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_centerInParent="true" />          <TextView              android:id="@+id/tv1"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_below="@id/pgBar"              android:layout_centerHorizontal="true"              android:text="正在登录..."              android:textColor="#000000"              android:textSize="18sp" />      </RelativeLayout>      <LinearLayout          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:layout_weight="1"          android:gravity="center"          android:orientation="vertical" >          <Button              android:id="@+id/btn_back"              android:layout_width="70dip"              android:layout_height="35dip"              android:text="取消"              android:textColor="#000000"              android:textSize="12sp" />      </LinearLayout>  
0 0
原创粉丝点击