android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
畢業(yè)剛開始上班接觸的第一個(gè)項(xiàng)目移動(dòng)護(hù)士站,接到了第一任務(wù)就是登錄,要用到自動(dòng)登錄功能,所以在這做個(gè)記錄,以后用的時(shí)候直接來粘貼復(fù)制,廢話少說,直奔主題
先上一下效果圖,由于只是實(shí)現(xiàn)功能,界面沒有美化,見諒
由于xml文件內(nèi)容,就不展現(xiàn)在這了,自己寫一寫就好,爸媽再也不用擔(dān)心我的學(xué)習(xí)了,so easy
package com.sdufe.login; 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.Menu; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; /** * @author lili.guo * * 2014-6-6下午3:20:17 */ public class MainActivity extends Activity { private EditText username_et; private EditText password_et; private CheckBox rem; private CheckBox auto; private Button login; private String username,password; SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp=getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE); username_et=(EditText) findViewById(R.id.username); password_et=(EditText) findViewById(R.id.password); rem=(CheckBox) findViewById(R.id.remember); auto=(CheckBox) findViewById(R.id.autologin); login=(Button) findViewById(R.id.login); if (rem.isChecked()) { username_et.setText(sp.getString("username", "")); password_et.setText(sp.getString("password", "")); if (auto.isChecked()) { Intent intent1=new Intent(); intent1.setClass(getApplicationContext(), Welcome.class); startActivity(intent1); } } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub username=username_et.getText().toString(); password=password_et.getText().toString(); if (username.equals("Thea")&&password.equals("123")) { Toast.makeText(getApplicationContext(), "登錄成功", Toast.LENGTH_SHORT).show(); if (rem.isChecked()) { Editor editor=sp.edit(); editor.putString("username", username); editor.putString("password", password); editor.commit(); } Intent intent2=new Intent(); intent2.setClass(getApplicationContext(), Welcome.class); startActivity(intent2); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
用戶名和密碼是寫死的,為了方便有需要的人學(xué)習(xí),稍微解釋一下
if (rem.isChecked()) { username_et.setText(sp.getString("username", "")); password_et.setText(sp.getString("password", "")); if (auto.isChecked()) { Intent intent1=new Intent(); intent1.setClass(getApplicationContext(), Welcome.class); startActivity(intent1); } }
以上代碼意思是如果記住密碼就拿到本地存儲(chǔ)的用戶名和密碼,如果是自動(dòng)登錄則直接跳轉(zhuǎn)的下一個(gè)網(wǎng)頁
if (rem.isChecked()) { Editor editor=sp.edit(); editor.putString("username", username); editor.putString("password", password); editor.commit(); } Intent intent2=new Intent(); intent2.setClass(getApplicationContext(), Welcome.class); startActivity(intent2);
以上代碼意思是說如果是記住密碼的狀態(tài),則把用戶名和密碼寫到本地
注意一點(diǎn)哈,跳轉(zhuǎn)到下一個(gè)activity時(shí),要修改一下AndroidManifest.xml文件,ok,結(jié)束。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android10 隱藏SystemUI鎖屏下的多用戶圖標(biāo)的示例代碼
- Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
- Android實(shí)現(xiàn)用戶圓形頭像和模糊背景
- Android實(shí)現(xiàn)簡單用戶注冊(cè)案例
- Android啟動(dòng)頁用戶相關(guān)政策彈框的實(shí)現(xiàn)代碼
- 詳解Android Studio實(shí)現(xiàn)用戶登陸界面demo(xml實(shí)現(xiàn))
- Android權(quán)限如何禁止以及友好提示用戶開通必要權(quán)限詳解
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android模擬用戶點(diǎn)擊的實(shí)現(xiàn)方法
- Android EditText 監(jiān)聽用戶輸入完成的實(shí)例
- Android 用戶Session管理的設(shè)計(jì)方案
- Android基于AlarmManager實(shí)現(xiàn)用戶在線心跳功能示例
- Android 多用戶詳情
相關(guān)文章
android自定義View實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了android自定義View實(shí)現(xiàn)跑馬燈效果,實(shí)現(xiàn)連續(xù)滾動(dòng)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Flutter 如何設(shè)置App的主色調(diào)與字體
App 開發(fā)過程中,肯定希望給用戶帶來一致的體驗(yàn),這其中最基礎(chǔ)的就是色調(diào)、字體保持一致。在 Flutter 中,可以設(shè)置全局的主題色調(diào)和字體,從而在其他頁面引用主色調(diào)和字體,實(shí)現(xiàn)頁面展示層面的一致。2021-05-05Kotlin淺析延遲初始化與密封類的實(shí)現(xiàn)方法
Kotlin語言的許多特性,包括變量不可變,變量不可為空,等等。這些特性都是為了盡可能地保證程序安全而設(shè)計(jì)的,但是有些時(shí)候這些特性也會(huì)在編碼時(shí)給我們帶來不少的麻煩,下面我們來了解延遲初始化和密封類的特點(diǎn)2022-08-08Android 基于RecyclerView實(shí)現(xiàn)的歌詞滾動(dòng)自定義控件
這篇文章主要介紹了Android 基于RecyclerView實(shí)現(xiàn)的歌詞滾動(dòng)自定義控件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03android 使用瀏覽器打開指定頁面的實(shí)現(xiàn)方法
這篇文章主要介紹了android 使用瀏覽器打開指定頁面的實(shí)現(xiàn)方法,本文通過實(shí)例文字說明的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序的深入分析
本篇文章是對(duì)Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android如何動(dòng)態(tài)改變App桌面圖標(biāo)
這篇文章主要介紹了 Android動(dòng)態(tài)改變App桌面圖標(biāo)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01基于移動(dòng)端真機(jī)調(diào)試的圖文教程(分享)
下面小編就為大家分享一篇基于移動(dòng)端真機(jī)調(diào)試的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12