Android實戰簡易教程-第六十六槍(結合SharedPreferenced實現自動登入功能)

來源:互聯網
上載者:User

標籤:android   自動登入   

我們使用的一般應用都有記住密碼、自動登入功能,這樣不用使用者每次都要點擊登入按鈕,提升使用者體驗,下面我們通過一個執行個體研究一下如何通過android的sharedpreferenced實現自動登入功能。本執行個體有三個介面-登入介面,跳轉介面,登入成功介面。

1.登入介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/logo_bg"    android:orientation="vertical" >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <ImageButton            android:id="@+id/img_btn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:background="@drawable/quit" />        <TextView            android:id="@+id/tv_zh"            android:layout_width="wrap_content"            android:layout_height="35dip"            android:layout_marginLeft="12dip"            android:layout_marginTop="10dip"            android:gravity="bottom"            android:text="帳號:"            android:textColor="#000000"            android:textSize="18sp" />        <EditText            android:id="@+id/et_zh"            android:layout_width="fill_parent"            android:layout_height="40dip"            android:layout_below="@id/tv_zh"            android:layout_marginLeft="12dip"            android:layout_marginRight="10dip" />        <TextView            android:id="@+id/tv_mima"            android:layout_width="wrap_content"            android:layout_height="35dip"            android:layout_below="@id/et_zh"            android:layout_marginLeft="12dip"            android:layout_marginTop="10dip"            android:gravity="bottom"            android:text="密碼:"            android:textColor="#000000"            android:textSize="18sp" />        <EditText            android:id="@+id/et_mima"            android:layout_width="fill_parent"            android:layout_height="40dip"            android:layout_below="@id/tv_mima"            android:layout_marginLeft="12dip"            android:layout_marginRight="10dip"            android:maxLines="200"            android:password="true"            android:scrollHorizontally="true" />        <CheckBox            android:id="@+id/cb_auto"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/et_mima"            android:layout_marginLeft="12dip"            android:text="自動登入"            android:textColor="#000000" />        <Button            android:id="@+id/btn_mima"            android:layout_width="100dip"            android:layout_height="40dip"            android:layout_below="@id/cb_auto"            android:layout_marginLeft="12dip"            android:gravity="center"            android:text="忘記密碼"            android:textColor="#000000"            android:textSize="18sp" />        <Button            android:id="@+id/btn_login"            android:layout_width="80dip"            android:layout_height="40dip"            android:layout_alignRight="@+id/et_mima"            android:layout_below="@+id/cb_auto"            android:gravity="center"            android:text="登入"            android:textColor="#000000"            android:textSize="18sp" />    </RelativeLayout></LinearLayout>

2.跳轉介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/logo_bg"    android:orientation="vertical" >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:layout_weight="3">        <ProgressBar            android:id="@+id/pgBar"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true" />        <TextView            android:id="@+id/tv1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/pgBar"            android:layout_centerHorizontal="true"            android:text="正在登入..."            android:textColor="#000000"            android:textSize="18sp" />    </RelativeLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:orientation="vertical" >        <Button            android:id="@+id/btn_back"            android:layout_width="70dip"            android:layout_height="35dip"            android:text="取消"            android:textColor="#000000"            android:textSize="12sp" />    </LinearLayout></LinearLayout>

3.成功介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_gravity="center"    android:background="@drawable/login_bg"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="登陸成功,進入使用者介面"        android:textColor="#000000"        android:textSize="20sp" /></LinearLayout>

下面看一下3個activity:

LoginActivity.java:

package com.liu.activity;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.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.ImageButton;import android.widget.Toast;public class LoginActivity extends Activity {private EditText userName, password;private CheckBox auto_login;private Button btn_login, btn_forget_pass;private ImageButton btnQuit;private String userNameValue, passwordValue;private SharedPreferences sp;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去除標題this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.login);// 獲得執行個體對象sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);userName = (EditText) findViewById(R.id.et_zh);password = (EditText) findViewById(R.id.et_mima);btn_forget_pass = (Button) findViewById(R.id.btn_mima);auto_login = (CheckBox) findViewById(R.id.cb_auto);btn_login = (Button) findViewById(R.id.btn_login);btnQuit = (ImageButton) findViewById(R.id.img_btn);// 判斷記住密碼多選框的狀態if (sp.getBoolean("AUTO_ISCHECK", false)) {// 設定預設是記錄密碼狀態userName.setText(sp.getString("USER_NAME", ""));password.setText(sp.getString("PASSWORD", ""));}else if(sp.getBoolean("AUTO_ISCHECK", true)){Intent intent = new Intent(LoginActivity.this,JumpActivity.class);LoginActivity.this.startActivity(intent);LoginActivity.this.finish();auto_login.setChecked(true);}// 登入監聽事件 現在預設為使用者名稱為:yayun 密碼:123btn_login.setOnClickListener(new OnClickListener() {public void onClick(View v) {userNameValue = userName.getText().toString();passwordValue = password.getText().toString();if (userNameValue.equals("yayun") && passwordValue.equals("123")) {Toast.makeText(LoginActivity.this, "登入成功",Toast.LENGTH_SHORT).show();// 登入成功和記住密碼框為選中狀態才儲存使用者資訊if (auto_login.isChecked()) {// 記住使用者名稱、密碼、Editor editor = sp.edit();editor.putString("USER_NAME", userNameValue);editor.putString("PASSWORD", passwordValue);editor.commit();}// 跳轉介面Intent intent = new Intent(LoginActivity.this,JumpActivity.class);LoginActivity.this.startActivity(intent);LoginActivity.this.finish();} else {Toast.makeText(LoginActivity.this, "使用者名稱或密碼錯誤,請重新登入",Toast.LENGTH_LONG).show();}}});// 監聽記住密碼多選框按鈕事件auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (auto_login.isChecked()) {System.out.println("記住密碼已選中");sp.edit().putBoolean("AUTO_ISCHECK", true).commit();} else {System.out.println("記住密碼沒有選中");sp.edit().putBoolean("AUTO_ISCHECK", false).commit();}}});btnQuit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {LoginActivity.this.finish();}});}}

2.JumpActivity.java:

package com.liu.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class JumpActivity extends Activity {private ProgressBar progressBar;private Button backButton;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去除標題this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.jump);progressBar = (ProgressBar) findViewById(R.id.pgBar);backButton = (Button) findViewById(R.id.btn_back);startWelcomeAvtivity();backButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {JumpActivity.this.finish();}});}private void startWelcomeAvtivity() {new Handler().postDelayed(new Runnable() {//延遲操作public void run() {Intent intent = new Intent(JumpActivity.this,WelcomeAvtivity.class);JumpActivity.this.startActivity(intent);JumpActivity.this.finish();}},2000);// 設定執行時間2秒}}
3.Welcome.java:

package com.liu.activity;import android.app.Activity;import android.os.Bundle;public class WelcomeAvtivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.welcome);}}
運行執行個體:

這時應該記錄住密碼,可以自行自動登入了,我們測試一下,退出應用後再次進入,如下:

如果你想更完善一些,可以自己做一個登出的操作哦!

喜歡的朋友關注我!
源碼下載

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android實戰簡易教程-第六十六槍(結合SharedPreferenced實現自動登入功能)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.