最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)登錄界面記住密碼的存儲

 更新時間:2022年04月23日 14:23:17   作者:ly1012053441  
這篇文章主要為大家詳細介紹了Android SharedPreferrences實現(xiàn)登錄界面記住密碼的存儲,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

一.布局界面

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);
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

宣化县| 白河县| 客服| 抚宁县| 绵竹市| 宜章县| 易门县| 探索| 巫溪县| 梅州市| 合山市| 宝山区| 瓦房店市| 双流县| 图片| 航空| 海晏县| 济南市| 陆良县| 巴彦淖尔市| 闻喜县| 昌邑市| 河源市| 天等县| 青铜峡市| 泉州市| 棋牌| 防城港市| 增城市| 始兴县| 沙湾县| 镇康县| 宁南县| 读书| 米脂县| 石泉县| 屏南县| 石林| 乌兰县| 梅州市| 措勤县|