android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
畢業(yè)剛開始上班接觸的第一個項(xiàng)目移動護(hù)士站,接到了第一任務(wù)就是登錄,要用到自動登錄功能,所以在這做個記錄,以后用的時(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); } }
以上代碼意思是如果記住密碼就拿到本地存儲的用戶名和密碼,如果是自動登錄則直接跳轉(zhuǎn)的下一個網(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)到下一個activity時(shí),要修改一下AndroidManifest.xml文件,ok,結(jié)束。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:android實(shí)現(xiàn)簡單計(jì)算器功能
欄 目:Android
下一篇:Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
本文標(biāo)題:android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
本文地址:http://mengdiqiu.com.cn/a1/Android/9207.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳