最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android SharedPreferences實現(xiàn)記住密碼和自動登錄

 更新時間:2019年05月27日 15:05:08   作者:橙汁丶  
這篇文章主要為大家詳細介紹了Android SharedPreferences實現(xiàn)記住密碼和自動登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android SharedPreferences實現(xiàn)記住密碼和自動登錄,供大家參考,具體內(nèi)容如下

效果圖:

第一次進入進來

勾選記住密碼和自動登錄成功后,第二次進來 

說明:中間存在的圖片或者多余的其他部分可刪掉。留下最主要的填寫部分和登陸按鈕即可。功能還是可以實現(xiàn)的。

 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);
  //調(diào)用數(shù)據(jù)庫
  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) {
    //回調(diào)信息,可以在這里獲取基本的授權(quán)返回的信息,但是注意如果做提示和UI操作要傳到主線程handler里去執(zhí)行
    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","已經(jīng)注冊過!");
      }else{
       sqLiteDatabase.execSQL("insert into dtb_users(uname,upass,name,levelnumber) values('"+openid+"','null','"+nickname+"','"+1+"')");
      }
      //跳轉(zhuǎn)
      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單獨調(diào)用一個即可
    weibo.authorize();//單獨授權(quán),OnComplete返回的hashmap是空的
    weibo.showUser(null);//授權(quán)并獲取用戶信息
    //移除授權(quán)
    // 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"));
   }
 
  }
 
  // 登錄監(jiān)聽事件 現(xiàn)在默認為用戶名為: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()){//判斷是否查詢到此數(shù)據(jù)
     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();
     //跳轉(zhuǎn)界面
     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下載:記住密碼和自動登錄

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Jetpack?Compose狀態(tài)專篇精講

    Jetpack?Compose狀態(tài)專篇精講

    在今年的Google/IO大會上,亮相了一個全新的?Android?原生?UI?開發(fā)框架-Jetpack?Compose,?與蘋果的SwiftIUI一樣,Jetpack?Compose是一個聲明式的UI框架,這篇文章主要介紹了Jetpack?Compose狀態(tài)管理
    2022-10-10
  • Android基于OpenCV實現(xiàn)霍夫直線檢測

    Android基于OpenCV實現(xiàn)霍夫直線檢測

    霍夫變換利用點與線之間的對偶性,將圖像空間中直線上離散的像素點通過參數(shù)方程映射為霍夫空間中的曲線,并將霍夫空間中多條曲線的交點作為直線方程的參數(shù)映射為圖像空間中的直線。給定直線的參數(shù)方程,可以利用霍夫變換來檢測圖像中的直線。本文簡單講解Android的實現(xiàn)
    2021-06-06
  • Android單個RecyclerView實現(xiàn)列表嵌套的效果

    Android單個RecyclerView實現(xiàn)列表嵌套的效果

    本篇文章主要介紹了Android單個RecyclerView實現(xiàn)列表嵌套的效果,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • Android Fragment源碼分析Add方法

    Android Fragment源碼分析Add方法

    Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應大屏幕的平板電腦, 當然現(xiàn)在他仍然是平板APP UI設計的寵兒,而且我們普通手機開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段
    2022-08-08
  • android ListActivity顯示圖標實例

    android ListActivity顯示圖標實例

    在ListActivity中顯示圖標,好像并不復雜,實現(xiàn)起來卻不輕松,我們下面一步步來實現(xiàn)ListActivity中顯示圖標
    2013-11-11
  • android自定義倒計時控件示例

    android自定義倒計時控件示例

    這篇文章主要介紹了Android秒殺倒計時自定義TextView示例,大家參考使用吧
    2014-01-01
  • Android開發(fā)中Bitmap高效加載使用詳解

    Android開發(fā)中Bitmap高效加載使用詳解

    在Android開發(fā)中,我們經(jīng)常與Bitmap打交道,而對Bitmap的不恰當?shù)牟僮鹘?jīng)常會導致OOM(Out of Memory)。這篇文章我們會介紹如何高效地在Android開發(fā)中使用Bitmap,在保證圖片顯示質(zhì)量的前提下盡可能占用更小的內(nèi)存。
    2017-12-12
  • Android實現(xiàn)多點觸控,自由縮放圖片的實例代碼

    Android實現(xiàn)多點觸控,自由縮放圖片的實例代碼

    本篇文章主要介紹了Android實現(xiàn)多點觸控,自由縮放圖片的實例代碼,可以自由地對圖片進行縮放和移動,非常具有實用價值,需要的朋友可以參考下。
    2016-12-12
  • Kotlin如何安全訪問lateinit變量的實現(xiàn)

    Kotlin如何安全訪問lateinit變量的實現(xiàn)

    這篇文章主要介紹了Kotlin如何安全訪問lateinit變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • Recyclerview添加頭布局和尾布局、item點擊事件詳解

    Recyclerview添加頭布局和尾布局、item點擊事件詳解

    這篇文章主要為大家詳細介紹了Recyclerview添加頭布局和尾布局、item點擊事件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論

虹口区| 邵武市| 都匀市| 凌源市| 山阳县| 登封市| 宁强县| 漳州市| 隆化县| 桂阳县| 平南县| 桃园市| 南雄市| 石泉县| 青海省| 辽中县| 河间市| 科技| 舟曲县| 行唐县| 叙永县| 新巴尔虎右旗| 安泽县| 兴化市| 彩票| 安阳市| 开远市| 虞城县| 黎川县| 施秉县| 万宁市| 罗平县| 嘉祥县| 延边| 中阳县| 从江县| 阜新市| 南木林县| 建阳市| 新竹县| 武陟县|