国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)用戶登錄記住密碼功能

2019-10-23 18:28:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、打開(kāi)之前完成的Case_login進(jìn)行修改再編輯

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

二、將注冊(cè)按鈕刪除并在登錄按鈕前拖出一個(gè)checkBox,編輯代碼如下:

在layout_top.xml文件中進(jìn)行編輯

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/activity_horizontal_margin" android:background="@drawable/logintop_roundbg"> <EditText  android:id="@+id/etName"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="@android:drawable/edit_text"  android:drawableLeft="@drawable/icon_user"  android:drawablePadding="10dp"  android:ems="10"  android:hint="@string/etName">  <requestFocus /> </EditText> <EditText  android:id="@+id/etPassword"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_below="@id/etName"  android:background="@android:drawable/edit_text"  android:drawableLeft="@drawable/icon_pass"  android:drawablePadding="10dp"  android:ems="10"  android:hint="@string/etPass"  android:inputType="textPassword">  <requestFocus /> </EditText> <LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_below="@+id/etPassword">  <Button   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1"   android:background="@drawable/btn_select"   android:text="@string/btnLogin" />  <Button   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1"   android:layout_marginLeft="10dp"   android:background="@drawable/btn_select"   android:text="@string/btnRegister" /> </LinearLayout></RelativeLayout>

效果圖如下:

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

三、對(duì)登錄密碼及記住密碼進(jìn)行編輯

在LoginAcitvity.java文件進(jìn)行編寫(xiě)修改,代碼如下:

package cn.edu.bzu.case_login;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox cbIsRememberPass; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_login);  initViews();  sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);  boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);  if(isRemember){   String name=sharedPreferences.getString("name","");   String password=sharedPreferences.getString("password","");   etName.setText(name);   etPassword.setText(password);   cbIsRememberPass.setChecked(true);  } } private void initViews(){  etName=(EditText)findViewById(R.id.etName);  etPassword=(EditText)findViewById(R.id.etPassword);  cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass); } public void login(View view){  String name=etName.getText().toString();  String password=etPassword.getText().toString();  if("admin".equals(name)&&"123456".equals(password)){   SharedPreferences.Editor editor=sharedPreferences.edit();   if(cbIsRememberPass.isChecked()){    editor.putBoolean("rememberpassword",true);    editor.putString("name",name);    editor.putString("password",password);   }   else {    editor.clear();   }   editor.commit();   Intent intent=new Intent(this,MainActivity.class);   startActivity(intent);   finish();  }  else {   Toast.makeText(this,"賬戶或密碼錯(cuò)誤",Toast.LENGTH_LONG).show();  } }}

四、設(shè)計(jì)登錄后的界面

新建一個(gè)MainActivity.java文件,同時(shí)生成一個(gè)activity_main.xml,對(duì)其進(jìn)行編寫(xiě)代碼效果圖如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="cn.edu.bzu.case_login.MainActivity"> <TextView  android:text="Welcome you"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_centerVertical="true"  android:layout_centerHorizontal="true"  android:textSize="40sp"  android:id="@+id/textView" /></RelativeLayout>

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

五、結(jié)果演示

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

再次打開(kāi)軟件密碼就記住了如圖:

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

輸入錯(cuò)誤的密碼有提示如下圖:

Android用戶登錄記住密碼,Android登錄記住密碼,Android記住密碼

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 株洲县| 高州市| 赤水市| 楚雄市| 丹阳市| 长垣县| 信丰县| 灵川县| 贺州市| 洛隆县| 宿松县| 于田县| 元朗区| 咸阳市| 景洪市| 台北县| 谷城县| 沂南县| 北辰区| 平南县| 衡东县| 永善县| 赤城县| 明溪县| 南江县| 工布江达县| 乐安县| 文登市| 白朗县| 江北区| 尚志市| 千阳县| 仁怀市| 佳木斯市| 固镇县| 西畴县| 资溪县| 永川市| 常熟市| 牙克石市| 满洲里市|