概述
本文实例为大家分享了Android SharedPreferences实现记住密码和自动登录,供大家参考,具体内容如下
效果图:
第一次进入进来
勾选记住密码和自动登录成功后,第二次进来
说明:中间存在的图片或者多余的其他部分可删掉。留下最主要的填写部分和登陆按钮即可。功能还是可以实现的。
XML文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bj" tools:context="com.example.application.MainActivity"> <ImageView android:layout_marginTop="50dp" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/login_tx_1" android:layout_gravity="center" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" android:hint="请输入账号" android:gravity="center" android:paddingRight="100dp" android:id="@+id/login_uname" /> <TextView android:layout_width="38dp" android:layout_height="33dp" android:layout_marginLeft="30dp" android:padding="6dp" android:gravity="center" android:drawableLeft="@drawable/uname" /> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true" android:hint="请输入密码" android:gravity="center" android:paddingRight="100dp" android:password="true" android:id="@+id/login_upass" /> <TextView android:layout_width="38dp" android:layout_height="33dp" android:layout_marginLeft="30dp" android:padding="6dp" android:gravity="center" android:drawableLeft="@drawable/upass" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" > <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:id="@+id/login_auto" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="自动登录" android:id="@+id/login_btn" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="15dp" > <ImageButton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_qq" /> <ImageButton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_weixin" android:layout_marginLeft="60dp" /> <ImageButton android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/login_xinlan" android:layout_marginLeft="60dp" android:id="@+id/login_xinlan" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/a_radio_button_selector_1" android:gravity="center" android:text="登录" android:id="@+id/login_login" /> <Button android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="@drawable/a_radio_button_selector_1" android:gravity="center" android:text="忘记密码" android:id="@+id/login_find" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="没有账号,立即注册" android:textColor="#6efafa" android:textSize="15dp" android:onClick="JumpRegister" /> </LinearLayout> </LinearLayout>
Java文件
package com.example.application; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; import com.sun.util.DBHelper; import java.util.HashMap; import cn.sharesdk.framework.Platform; import cn.sharesdk.framework.PlatformActionListener; import cn.sharesdk.framework.ShareSDK; import cn.sharesdk.sina.weibo.SinaWeibo; public class MainActivity extends AppCompatActivity { public static String LoginUid=null; public static String LoginName=null; private Button login_login; private Button login_find; private EditText login_uname; private EditText login_upass; private CheckBox login_auto; private CheckBox login_btn; private SharedPreferences sp; private ImageButton login_xinlan; private Platform weibo; private DBHelper dbHelper; private SQLiteDatabase sqLiteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //获取控件 login_uname = (EditText) findViewById(R.id.login_uname); login_upass = (EditText) findViewById(R.id.login_upass); login_auto = (CheckBox) findViewById(R.id.login_auto); //记住密码 login_btn = (CheckBox) findViewById(R.id.login_btn); //自动登录 login_login = (Button) findViewById(R.id.login_login); //登录 login_find = (Button) findViewById(R.id.login_find); login_xinlan = (ImageButton) findViewById(R.id.login_xinlan); //调用数据库 dbHelper = new DBHelper(this,"dtb.db",null,1); sqLiteDatabase = dbHelper.getWritableDatabase(); //第三方登录 weibo = ShareSDK.getPlatform(SinaWeibo.NAME); login_xinlan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //回调信息,可以在这里获取基本的授权返回的信息,但是注意如果做提示和UI操作要传到主线程handler里去执行 weibo.setPlatformActionListener(new PlatformActionListener () { @Override public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) { String openid = platform.getDb().getUserId(); String nickname = platform.getDb().getUserName(); Cursor cursor=sqLiteDatabase.rawQuery("select * from dtb_users where uname=?",new String[]{openid}); LoginUid=cursor.getString(cursor.getColumnIndex("uid")); if(cursor.moveToNext()){ Log.i("test","已经注册过!"); }else{ sqLiteDatabase.execSQL("insert into dtb_users(uname,upass,name,levelnumber) values('"+openid+"','null','"+nickname+"','"+1+"')"); } //跳转 MainActivity.LoginName=openid; Intent intent=new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); } @Override public void onError(Platform arg0, int arg1, Throwable arg2) { // TODO Auto-generated method stub arg2.printStackTrace(); } @Override public void onCancel(Platform arg0, int arg1) { // TODO Auto-generated method stub } }); //authorize与showUser单独调用一个即可 weibo.authorize();//单独授权,OnComplete返回的hashmap是空的 weibo.showUser(null);//授权并获取用户信息 //移除授权 // weibo.removeAccount(true); } }); //自动登录判断 sp = this.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); //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码 if(choseRemember){ login_uname.setText(name); login_upass.setText(pass); login_auto.setChecked(true); } //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录 if(choseAutoLogin){ login_btn.setChecked(true); Cursor cursor= sqLiteDatabase.rawQuery("select * from dtb_users where uname=? and upass=?",new String[]{name,pass}); if(cursor.moveToNext()){ new LoginThread().start(); LoginName=name; LoginUid=cursor.getString(cursor.getColumnIndex("uid")); } } // 登录监听事件 现在默认为用户名为:admin 密码:123 login_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName=login_uname.getText().toString(); String userPass=login_upass.getText().toString(); SharedPreferences.Editor editor =sp.edit(); Cursor cursor= sqLiteDatabase.rawQuery("select * from dtb_users where uname=? and upass=?",new String[]{userName,userPass}); if(cursor.moveToNext()){//判断是否查询到此数据 Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show(); LoginName=userName; LoginUid=cursor.getString(cursor.getColumnIndex("uid")); //是否记住密码 //记住用户名、密码、 editor.putString("USER_NAME", userName); editor.putString("PASSWORD",userPass); if(login_auto.isChecked()){ editor.putBoolean("remember", true); }else{ editor.putBoolean("remember", false); } //是否自动登录 if(login_btn.isChecked()){ editor.putBoolean("autologin", true); }else{ editor.putBoolean("autologin", false); } editor.commit(); //跳转界面 Intent intent = new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_SHORT).show(); // finish(); }else{ Toast.makeText(MainActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show(); } } }); } public void JumpRegister(View view){ Intent intent=new Intent(this,RegisterActivity.class); startActivity(intent); finish(); } //子线程 控制自动睡眠2秒钟后自动登录 class LoginThread extends Thread{ @Override public void run() { try { sleep(2000); Intent intent = new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Demo下载:记住密码和自动登录
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是天真羽毛为你收集整理的Android SharedPreferences实现记住密码和自动登录的全部内容,希望文章能够帮你解决Android SharedPreferences实现记住密码和自动登录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复