android如何實(shí)現(xiàn)記住用戶名和密碼以及自動登錄

這篇文章給大家分享的是有關(guān)android如何實(shí)現(xiàn)記住用戶名和密碼以及自動登錄的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

“真誠服務(wù),讓網(wǎng)絡(luò)創(chuàng)造價值”是我們的服務(wù)理念,創(chuàng)新互聯(lián)團(tuán)隊(duì)十余年如一日始終堅持在網(wǎng)站建設(shè)領(lǐng)域,為客戶提供優(yōu)質(zhì)服。不管你處于什么行業(yè),助你輕松跨入“互聯(lián)網(wǎng)+”時代,PC網(wǎng)站+手機(jī)網(wǎng)站+公眾號+成都微信小程序

先上一下效果圖,由于只是實(shí)現(xiàn)功能,界面沒有美化,見諒

android如何實(shí)現(xiàn)記住用戶名和密碼以及自動登錄

android如何實(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時,要修改一下AndroidManifest.xml文件,ok,結(jié)束。

感謝各位的閱讀!關(guān)于“android如何實(shí)現(xiàn)記住用戶名和密碼以及自動登錄”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

當(dāng)前名稱:android如何實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
文章來源:http://muchs.cn/article4/pphdie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站設(shè)計域名注冊、網(wǎng)站制作營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)