Android:利用SharedPreferences实现自动登录
2016-05-23 10:57
621 查看
主要代码:
public class LoginActivity extends Activity { private EditText username; private EditText userpassword; private CheckBox remember; private CheckBox autologin; private Button login; private SharedPreferences sp; private String userNameValue,passwordValue; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); // 初始化用户名、密码、记住密码、自动登录、登录按钮 username = (EditText) findViewById(R.id.username); userpassword = (EditText) findViewById(R.id.userpassword); remember = (CheckBox) findViewById(R.id.remember); autologin = (CheckBox) findViewById(R.id.autologin); login = (Button) findViewById(R.id.login); sp = getSharedPreferences("userInfo", 0); String name=sp.getString("USER_NAME", ""); String pass =sp.getString("PASSWORD", ""); boolean choseRemember =sp.getBoolean("remember", false); boolean choseAutoLogin =sp.getBoolean("autologin", false); // Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码 if(choseRemember){ username.setText(name); userpassword.setText(pass); remember.setChecked(true); } //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录 if(choseAutoLogin){ autologin.setChecked(true); } login.setOnClickListener(new OnClickListener() { // 默认可登录帐号tinyphp,密码123 @Override public void onClick(View arg0) { userNameValue = username.getText().toString(); passwordValue = userpassword.getText().toString(); SharedPreferences.Editor editor =sp.edit(); // TODO Auto-generated method stub if (userNameValue.equals("tinyphp") && passwordValue.equals("123")) { Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); //保存用户名和密码 editor.putString("USER_NAME", userNameValue); editor.putString("PASSWORD", passwordValue); //是否记住密码 if(remember.isChecked()){ editor.putBoolean("remember", true); }else{ editor.putBoolean("remember", false); } //是否自动登录 if(autologin.isChecked()){ editor.putBoolean("autologin", true); }else{ editor.putBoolean("autologin", false); } editor.commit(); //跳转 Intent intent =new Intent(LoginActivity.this,SuccessActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录!", Toast.LENGTH_SHORT).show(); } } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" > </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="密码:" /> <EditText android:id="@+id/userpassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" > </EditText> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /> <CheckBox android:id="@+id/autologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" /> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout>
相关文章推荐
- Patrol 7 架构下?的处理方法
- 清除IE分级审查密码
- 华为路由器密码恢复
- 注册表导出管理员密码文件
- 路由器版本与密码恢复
- 密码知识教程一
- 密码破解全教程
- 通过批处理修改FTP账号和密码
- redis中修改配置文件中的端口号 密码方法
- jQuery根据ID获取input、checkbox、radio、select的示例
- jquery实现的代替传统checkbox样式插件
- 更改Mysql root用户密码
- C#获取动态生成的CheckBox值
- C#判断密码强度的方法
- C#使用伪随机数实现加密用户密码的方法
- Mysql5.6 忘记root密码的解决办法
- Android控件之CheckBox、RadioButton用法实例分析
- C#修改及重置电脑密码DirectoryEntry实现方法
- MySql5.5忘记root密码怎么办
- C#实现在控制台输入密码显示星号的方法