自动登陆
来源:互联网 发布:ip网络代理服务器 编辑:程序博客网 时间:2024/06/11 02:31
1,进入界面,从SharedPreferences中获得 记住密码的状态ischeck和自动登录isAuto。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 {/* */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;@Overrideprotected 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() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 blocke.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() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean bool) {// TODO Auto-generated method stubif(bool){//自动登录选中,记住密码也选中。checkpass.setChecked(true);}}});//记住密码监听器checkpass.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean bool) {// TODO Auto-generated method stubif(!bool){auto_login.setChecked(false);//防止 在正常登录后,在次进入登录界面时,只做了取消记住密码操作,而没有登录的情况。editor.putInt("check",0);editor.putInt("auto", 0);editor.commit();}}});//登录事件login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(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 stubuser =(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);}@Overridepublic 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;}//在登录后,按下返回键@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.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);}}
0 0
- 自动登陆
- 呵呵,开机自动登陆,注销自动登陆
- 传神自动登陆研究
- FTP自动登陆脚本
- windows自动登陆
- 系统自动登陆
- Windows XP自动登陆
- WinXP自动登陆
- windows xp 自动登陆
- SecureCRT自动登陆脚本
- 自动登陆Windows 2003
- xp 自动登陆
- VSS的自动登陆
- XP自动登陆
- xp自动登陆方法
- 设置用户自动登陆
- 自动登陆Win03
- 登陆系统自动注销
- unix网络编程(二)Tcp:传输控制协议
- 关于在MFC中画圆的示例
- 参考jquery实现empty(清空子节点)
- myeclipse部署时An internal error occurred 错误的几种情况
- 理解MySQL——并行数据库与分区(Partition)
- 自动登陆
- 关于Oracle远程连接的配置
- StringIndexOutOfBoundsException
- Android广播机制
- MiniTwitter记住密码等功能实现
- 主动呼叫KVO(NSKeyValueObserverNotification)
- 毕业纪念
- jquery金额小写转大写
- JiBx:BindGen命令行--通过Java POJO对象产生bind.xml和xsd文件