您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关怎么在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(); } } } }
以上就是怎么在Android中使用SharedPreferences实现记住密码和自动登录,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。