Android實(shí)現(xiàn)登錄界面記住密碼的存儲(chǔ)
Android存儲(chǔ)方式有很多種,在這里所用的存儲(chǔ)方式是SharedPreferrences,其采用了Map數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)數(shù)據(jù),以鍵值的方式存儲(chǔ),可以簡(jiǎn)單的讀取與寫入。所以比較適合我們今天做的這個(gè)項(xiàng)目。我們來(lái)看一下運(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,"賬號(hào)或密碼有誤",Toast.LENGTH_LONG).show();
}
}
}
三.WelcomeActivity
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)記住密碼功能
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android開發(fā)筆記SQLite優(yōu)化記住密碼功能
- Android實(shí)現(xiàn)用戶登錄記住密碼功能
- Android sharedPreferences實(shí)現(xiàn)記住密碼功能
- Android 使用SharedPreferrences儲(chǔ)存密碼登錄界面記住密碼功能
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
- Android通過(guò)記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
- Android實(shí)現(xiàn)登陸界面的記住密碼功能
相關(guān)文章
flutter 自定義websocket路由的實(shí)現(xiàn)
這篇文章主要介紹了flutter 自定義websocket路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android SwipeRefreshLayout下拉刷新組件示例
SwipeRefrshLayout是Google官方更新的一個(gè)Widget,可以實(shí)現(xiàn)下拉刷新的效果。本文主要介紹了Android之SwipeRefreshLayout下拉刷新組件示例,有興趣的可以了解一下。2017-02-02
Android獲取SD卡路徑及SDCard內(nèi)存的方法
這篇文章主要介紹了Android獲取SD卡路徑及SDCard內(nèi)存的方法,較為詳細(xì)的分析了Android針對(duì)SD卡操作所涉及的類及其具體函數(shù)功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
Android之使用Android-query框架開發(fā)實(shí)戰(zhàn)(二)
這篇文章主要介紹了Android之使用Android-query框架開發(fā)實(shí)戰(zhàn)(二)的相關(guān)資料,需要的朋友可以參考下2015-10-10
Androd自定義對(duì)話框Dialog視圖及參數(shù)傳遞的實(shí)現(xiàn)方法
這篇文章主要介紹了Androd自定義對(duì)話框Dialog視圖及參數(shù)傳遞的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android開發(fā) -- UI界面之threme和style
做Java的人一般都做過(guò)CSS,我們都知道它也有一個(gè)樣式,Android中的樣式也可以進(jìn)行類比。2016-06-06
Android中的sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例
今天小編就為大家分享一篇關(guān)于Android中的sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Android應(yīng)用中使用Fragment組件的一些問題及解決方案總結(jié)
這里我們講的Fragment主要探討的是support庫(kù)中的Fragment,包括Fragment常遇到的crash崩潰問題,嵌套Fragment收不到onActivityResult()回調(diào)以及一些常用tips等,需要的朋友可以參考下2016-05-05
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05

