您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關android如何實現(xiàn)記住用戶名和密碼以及自動登錄的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
先上一下效果圖,由于只是實現(xiàn)功能,界面沒有美化,見諒
由于xml文件內容,就不展現(xiàn)在這了,自己寫一寫就好,爸媽再也不用擔心我的學習了,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; } }
用戶名和密碼是寫死的,為了方便有需要的人學習,稍微解釋一下
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); } }
以上代碼意思是如果記住密碼就拿到本地存儲的用戶名和密碼,如果是自動登錄則直接跳轉的下一個網頁
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),則把用戶名和密碼寫到本地
注意一點哈,跳轉到下一個activity時,要修改一下AndroidManifest.xml文件,ok,結束。
感謝各位的閱讀!關于“android如何實現(xiàn)記住用戶名和密碼以及自動登錄”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。