欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 使用SharedPreferrences儲存密碼登錄界面記住密碼功能

 更新時間:2017年04月12日 08:48:23   作者:ly1012053441  
Android存儲方式有很多種,在這里所用的存儲方式是SharedPreferrences, 其采用了Map數(shù)據(jù)結(jié)構(gòu)來存儲數(shù)據(jù),以鍵值的方式存儲,可以簡單的讀取與寫入,下面通過實例代碼給大家講解下,需要的朋友參考下吧

Android存儲方式有很多種,在這里所用的存儲方式是SharedPreferrences, 其采用了Map數(shù)據(jù)結(jié)構(gòu)來存儲數(shù)據(jù),以鍵值的方式存儲,可以簡單的讀取與寫入。所以比較適合我們今天做的這個項目。我們來看一下運(yùn)行圖:

一.布局界面

1.login_top.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 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:ems="10"
 android:drawablePadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableLeft="@drawable/icon_user"
 android:hint="@string/etName">
 <requestFocus></requestFocus>
 </EditText>
 <EditText
 android:id="@+id/etPassword"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/etName"
 android:inputType="textPassword"
 android:ems="10"
 android:drawablePadding="10dp"
 android:background="@android:drawable/edit_text"
 android:drawableLeft="@drawable/icon_pass"
 android:hint="@string/etpassword">
 <requestFocus></requestFocus>
 </EditText>
 <CheckBox
 android:id="@+id/cbremenber"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/etPassword"
 android:text="@string/cbpass"/>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/cbremenber">
 <Button
  android:id="@+id/btnlogin"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnlogin"
  android:onClick="login"/>
 <Button
  android:id="@+id/btnRegister"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/btnselect"
  android:text="@string/btnRegister"
  android:layout_marginLeft="10dp"/>
 </LinearLayout>
</RelativeLayout>

2.activity_main.xml

 <?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:background="@drawable/loginbg"
 tools:context="cn.edu.bzu.logindemo.MainActivity">
 <include layout="@layout/login_top"></include>
 <ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/deer"
 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true" />
</RelativeLayout>

3.activity_welcome.xml

<?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_welcome"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="cn.edu.bzu.logindemo.WelcomeActivity">
 <TextView
 android:id="@+id/tvwelcome"
 android:text="Welcome you"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="200dp"
 android:textSize="40sp"
  />
</RelativeLayout>

二.MainActivity

public class MainActivity extends AppCompatActivity {
 private EditText etName;
 private EditText etPassword;
 private SharedPreferences sharedPreferences;
 private CheckBox cbremenber;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initViews();
 sharedPreferences=getSharedPreferences("remenberpassword", Context.MODE_PRIVATE);
 boolean isRemember=sharedPreferences.getBoolean("remenberpassword",false);
 if(isRemember) {
  String name = sharedPreferences.getString("name", "");
  String password = sharedPreferences.getString("password", "");
  etName.setText(name);
  etPassword.setText(password);
  cbremenber.setChecked(true);
 }
 }
 private void initViews() {
 etName=(EditText) findViewById(R.id.etName);
 etPassword=(EditText) findViewById(R.id.etPassword);
 cbremenber=(CheckBox)findViewById(R.id.cbremenber);
 }
 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(cbremenber.isChecked()){
  editor.putBoolean("remenberpassword",true);
  editor.putString("name",name);
  editor.putString("password",password);
  }else {
  editor.clear();
  }
  editor.commit();
  Intent intent=new Intent(this,WelcomeActivity.class);
  startActivity(intent);
  finish();
 }else {
  Toast.makeText(this,"賬號或密碼有誤",Toast.LENGTH_LONG).show();
 }
 }
}

三.WelcomeActivity

 public class WelcomeActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_welcome);
 }
}

以上所述是小編給大家介紹的Android 使用SharedPreferrences儲存密碼登錄界面記住密碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android中獲取手機(jī)屏幕大小的方法

    Android中獲取手機(jī)屏幕大小的方法

    這篇文章主要介紹了Android中獲取手機(jī)屏幕大小的方法,Android開發(fā)需要獲得屏幕的寬高,本文為大家解析 Android中如何獲取手機(jī)屏幕大小,需要的朋友可以參考下
    2015-12-12
  • Android實現(xiàn)傾斜角標(biāo)樣式

    Android實現(xiàn)傾斜角標(biāo)樣式

    最新小編接到這樣一個項目,需要在一個距形卡片上做一個傾斜的Tag,類似支付寶上的一個功能,接著小編給大家?guī)砹藢崿F(xiàn)思路,對android 傾斜角標(biāo)的實現(xiàn)方法感興趣的朋友跟隨小編一起看看吧
    2019-10-10
  • Android自定義UI之粒子效果

    Android自定義UI之粒子效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義UI之粒子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android實現(xiàn)通知欄透明的方法

    Android實現(xiàn)通知欄透明的方法

    這個特性是andorid4.4支持的,最少要api19才可以使用,也就是說如果Android的機(jī)子是低于4.4,沉浸通知欄是沒有效果的。下面介紹一下使用的方法,非常得簡單,對android通知欄透明相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android高級開發(fā)之性能優(yōu)化典范

    Android高級開發(fā)之性能優(yōu)化典范

    本文從電量,視圖,內(nèi)存三個性能方面的知識點(diǎn)給大家介紹android高級開發(fā)之性能優(yōu)化的相關(guān)知識,希望對大家有所幫助
    2016-05-05
  • 深入Android HandlerThread 使用及其源碼完全解析

    深入Android HandlerThread 使用及其源碼完全解析

    這篇文章主要介紹了深入Android HandlerThread 使用及其源碼完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 深入理解與運(yùn)行Android Jetpack組件之ViewModel

    深入理解與運(yùn)行Android Jetpack組件之ViewModel

    ViewModel是Android Jetpack組件之一,是一種用于管理UI相關(guān)數(shù)據(jù)的架構(gòu)組件,它能夠幫助開發(fā)者實現(xiàn)優(yōu)雅的數(shù)據(jù)驅(qū)動和生命周期管理,本文將深入淺出地介紹ViewModel的使用和原理,帶你一步步掌握這個強(qiáng)大的組件
    2023-08-08
  • Android編程自定義對話框(Dialog)位置及大小的方法

    Android編程自定義對話框(Dialog)位置及大小的方法

    這篇文章主要介紹了Android編程自定義對話框(Dialog)位置及大小的方法,涉及Android對話框的定義、功能、屬性及布局相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android調(diào)用外置攝像頭的方法

    Android調(diào)用外置攝像頭的方法

    這篇文章主要為大家詳細(xì)介紹了Android調(diào)用外置攝像頭的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android手機(jī)拍照或選取圖庫圖片作為頭像

    Android手機(jī)拍照或選取圖庫圖片作為頭像

    這篇文章主要介紹了Android手機(jī)拍照或選取圖庫圖片作為頭像的相關(guān)資料,需要的朋友可以參考下
    2015-06-06

最新評論