Android實(shí)現(xiàn)記住用戶名和密碼功能
Android 實(shí)現(xiàn)記住用戶名和密碼的功能是通過SharedPreference 存儲(chǔ)來實(shí)現(xiàn)的。創(chuàng)建一個(gè)復(fù)選按鈕,通過按鈕的否選取來進(jìn)行事件處理。若按鈕選中存儲(chǔ)賬號和密碼的信息。若按鈕沒有選中,則清空賬號和密碼的信息。
結(jié)果演示:

源代碼下載地址:
https://github.com/GXS1225/Android————-.git
分析
(1)判斷是否輸入了賬號和密碼
if(name.trim().equals("")){
Toast.makeText(this, "請您輸入用戶名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this, "請您輸入密碼!", Toast.LENGTH_SHORT).show();
return;
}
(2)在layout_main.xml定義一個(gè) CheckBox,進(jìn)行事件處理
//通過
boolean CheckBoxLogin = checkbox.isChecked();
//按鈕被選中,下次進(jìn)入時(shí)會(huì)顯示賬號和密碼
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("auto", true);
editor.commit();
}
//按鈕被選中,清空賬號和密碼,下次進(jìn)入時(shí)會(huì)顯示賬號和密碼
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("auto", false);
editor.commit();
}
(3) SharedPreference 的存儲(chǔ)實(shí)現(xiàn)
//先定義
SharedPreferences sp = null;
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
//對uname 和 upswd 的操作
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
(4)跳轉(zhuǎn)到Content.java界面
//Intent跳轉(zhuǎn) Intent intent = new Intent(Welcome.this,Content.class); startActivity(intent); finish();
步驟:
先寫一個(gè)登陸的界面: layout_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ADD8E6">
<RelativeLayout
android:id="@+id/login_div"
android:layout_width="fill_parent"
android:layout_height="221dp"
android:layout_margin="15dip"
android:background="@drawable/btn_bg"
android:padding="15dip" >
<TextView
android:id="@+id/login_user_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="@string/user"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/user_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_user_input"
android:background="@android:drawable/editbox_background"
android:inputType="text"
android:singleLine="true" />
<TextView
android:id="@+id/login_pass_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_input"
android:layout_margin="5dp"
android:text="@string/pass"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/pass_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_pass_input"
android:background="@android:drawable/editbox_background"
android:inputType="textPassword"
android:singleLine="true" />
<CheckBox
android:id="@+id/checkBoxLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pass_input"
android:layout_alignParentBottom="true"
android:text="@string/no_user" />
<Button
android:id="@+id/new_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/pass_input"
android:layout_marginRight="28dp"
android:onClick="To_Title"
android:text="@string/new_user" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="402dp"
android:layout_height="51dp"
android:layout_marginLeft="50dp"
android:background="@drawable/gxs_ziti" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:onClick="To_fruist"
android:background="@drawable/gxs2"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
</LinearLayout>
Welcome.java
package com.gxs.login;
import com.example.login.R;
import com.gxs.listview.*;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Welcome extends Activity implements OnClickListener{
private EditText uname = null;
private EditText upswd = null;
private CheckBox checkboxButton = null;
private Button login = null;
SharedPreferences sp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
init();
}
public void init()
{
uname = (EditText) findViewById(R.id.user_input);
upswd = (EditText) findViewById(R.id.pass_input);
checkboxButton = (CheckBox) findViewById(R.id.checkBoxLogin);
login = (Button) findViewById(R.id.new_user);
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == login){
String name = uname.getText().toString();
String pswd = upswd.getText().toString();
if(name.trim().equals("")){
Toast.makeText(this,
"請您輸入用戶名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this,
"請您輸入密碼!", Toast.LENGTH_SHORT).show();
return;
}
boolean CheckBoxLogin = checkboxButton.isChecked();
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("checkboxBoolean", true);
editor.commit();
}
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("checkboxBoolean", false);
editor.commit();
}
//Intent跳轉(zhuǎn)
Intent intent=new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();
}
}
}
Content.java
package com.gxs.listview;
import java.util.List;
import com.example.login.R;
import com.gxs.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Content extends Activity{
private ListView listview_fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
}
content.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Welcome" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="內(nèi)容" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
更多內(nèi)容請參考專題:Android密碼使用教程
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會(huì)有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10
將cantk runtime嵌入到現(xiàn)有的APP中的方法
今天小編就為大家分享一篇關(guān)于將cantk runtime嵌入到現(xiàn)有的APP中的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android布局技巧之創(chuàng)建可重用的UI組件
這篇文章主要為大家詳細(xì)介紹了Android布局技巧之創(chuàng)建可重用的UI組件,文中提到了include標(biāo)簽的使用方法,感興趣的小伙伴們可以參考一下2016-05-05
Android Studio 新建項(xiàng)目通過git上傳到碼云圖文教程詳解
本文通過圖文并茂的方式給大家介紹了Android Studio 新建項(xiàng)目通過git上傳到碼云的方法,需要的朋友可以參考下2017-11-11
Android系統(tǒng)自帶樣式 (android:theme)
Android系統(tǒng)中自帶樣式分享,需要的朋友可以參考下2013-01-01
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android中ActionBar以及menu的代碼設(shè)置樣式
這篇文章主要介紹了Android中ActionBar以及menu的代碼設(shè)置樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07
Android使用addView動(dòng)態(tài)添加組件的方法
這篇文章主要為大家詳細(xì)介紹了Android使用addView動(dòng)態(tài)添加組件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

