android实现记住用户名和密码以及实现自动登录
来源:互联网 发布:c语言 整数奇偶排序 编辑:程序博客网 时间:2024/05/01 11:38
毕业刚开始上班接触的第一个项目移动护士站,接到了第一任务就是登录,要用到自动登录功能,所以在这做个记录,以后用的时候直接来粘贴复制,废话少说,直奔主题
先上一下效果图,由于只是实现功能,界面没有美化,见谅
由于xml文件内容,就不展现在这了,自己写一写就好,爸妈再也不用担心我的学习了,so easy
package com.sdufe.login;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;/** * @author lili.guo * * 2014-6-6下午3:20:17 */public class MainActivity extends Activity {private EditText username_et;private EditText password_et;private CheckBox rem;private CheckBox auto;private Button login;private String username,password;SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sp=getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE);username_et=(EditText) findViewById(R.id.username);password_et=(EditText) findViewById(R.id.password);rem=(CheckBox) findViewById(R.id.remember);auto=(CheckBox) findViewById(R.id.autologin);login=(Button) findViewById(R.id.login);if (rem.isChecked()) {username_et.setText(sp.getString("username", ""));password_et.setText(sp.getString("password", ""));if (auto.isChecked()) {Intent intent1=new Intent();intent1.setClass(getApplicationContext(), Welcome.class);startActivity(intent1);}}login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubusername=username_et.getText().toString();password=password_et.getText().toString();if (username.equals("Thea")&&password.equals("123")) {Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();if (rem.isChecked()) {Editor editor=sp.edit();editor.putString("username", username);editor.putString("password", password);editor.commit();}Intent intent2=new Intent();intent2.setClass(getApplicationContext(), Welcome.class);startActivity(intent2);}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
用户名和密码是写死的,为了方便有需要的人学习,稍微解释一下
if (rem.isChecked()) {username_et.setText(sp.getString("username", ""));password_et.setText(sp.getString("password", ""));if (auto.isChecked()) {Intent intent1=new Intent();intent1.setClass(getApplicationContext(), Welcome.class);startActivity(intent1);}}
以上代码意思是如果记住密码就拿到本地存储的用户名和密码,如果是自动登录则直接跳转的下一个网页
if (rem.isChecked()) {Editor editor=sp.edit();editor.putString("username", username);editor.putString("password", password);editor.commit();}Intent intent2=new Intent();intent2.setClass(getApplicationContext(), Welcome.class);startActivity(intent2);以上代码意思是说如果是记住密码的状态,则把用户名和密码写到本地
注意一点哈,跳转到下一个activity时,要修改一下AndroidManifest.xml文件,ok,结束
1 0
- android实现记住用户名和密码以及实现自动登录
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android自动登录 记住用户名密码实现 SharedPreferences
- Android实现自动登录和记住密码
- 登录界面如何实现记住用户名和密码 且实现自动登录
- 让PLSQL记住曾经登录过的用户名和密码,实现自动登录
- Jquery 实现 “下次自动登录” 记住用户名密码功能
- Jquery 实现 “下次自动登录” 记住用户名密码效能
- Jquery 实现 “下次自动登录” 记住用户名密码功能
- 利用JQuery记住用户名及密码以实现自动登录
- 利用JQuery记住用户名及密码以实现自动登录
- Android实现登录名自动提示和记住密码功能
- Android 记住密码和自动登录界面的实现
- Android 记住密码和自动登录界面的实现
- Android 记住密码和自动登录界面的实现
- Android 记住密码和自动登录界面的实现
- iOS下的UILocalNotification的使用
- java的几种常用的排序
- 计算器实验
- 有胃病怎么吃最好
- python操作mysql数据库(常用函数)
- android实现记住用户名和密码以及实现自动登录
- uml类图关系
- 一个用 C++ 实现的快速无锁队列
- 浙大PAT考试1005~1008
- 两表连接
- list_entry和list_for_each分析
- struct inode和struct file
- 以太网及IEEE802封装
- Spring注解大全以及案例