标签:
1.运行后界面图
2.主要代码:
2.1 activity_main.xml(2个TextView 2个EditText 1个CheckBox以及1个Button):
1 <TextView 2 android:id="@+id/tvAccount" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_alignBaseline="@+id/etAccount" 6 android:layout_alignBottom="@+id/etAccount" 7 android:text="@string/tvAccount" 8 android:textAppearance="?android:attr/textAppearanceLarge" /> 9 10 <EditText 11 android:id="@+id/etAccount" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_alignParentRight="true" 15 android:layout_alignParentTop="true" 16 android:layout_marginTop="18dp" 17 android:ems="10" /> 18 19 <EditText 20 android:id="@+id/etPass" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_alignBottom="@+id/tvPass" 24 android:layout_alignLeft="@+id/etAccount" 25 android:ems="10" 26 android:inputType="textPassword" > 27 28 <requestFocus /> 29 </EditText> 30 31 <TextView 32 android:id="@+id/tvPass" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_alignLeft="@+id/tvAccount" 36 android:layout_below="@+id/etAccount" 37 android:layout_marginTop="29dp" 38 android:text="@string/tvPass" 39 android:textAppearance="?android:attr/textAppearanceLarge" /> 40 41 <CheckBox 42 android:id="@+id/cbRemPass" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_alignLeft="@+id/tvPass" 46 android:layout_below="@+id/etPass" 47 android:layout_marginTop="20dp" 48 android:text="@string/cbRemPass" /> 49 50 <Button 51 android:id="@+id/btnLogin" 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:layout_alignBottom="@+id/cbRemPass" 55 android:layout_alignRight="@+id/etPass" 56 android:text="@string/btnLogin" />
2.2 MainActivity.java
主要用了SharedPreferences来保存密码
2.2.1 定义SharedPreferences
private SharedPreferences mSettings = null;
2.2.2 button按钮的点击监听事件
1 private void setListener() { 2 // TODO Auto-generated method stub 3 btnLogin.setOnClickListener(new OnClickListener() { 4 public void onClick(View v) { 5 // TODO Auto-generated method stub 6 // 判断复选框是否被选中 7 if (cbRemPass.isChecked()) { 8 mSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 9 Editor edit = mSettings.edit(); 10 //标记 11 edit.putBoolean("isKeep", true); 12 //记录用户名 13 edit.putString("username", etAccount.getText().toString()); 14 edit.putString("password", etPass.getText().toString()); 15 edit.commit(); 16 }else{ 17 mSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 18 Editor edit = mSettings.edit(); 19 //标记 20 edit.putBoolean("isKeep", true); 21 //记录用户名 22 edit.putString("username", ""); 23 edit.putString("password", ""); 24 edit.commit(); 25 } 26 } 27 }); 28 }
2.2.3 第二次启动获取保存的密码的关键代码
1 private void getData() { 2 // TODO Auto-generated method stub 3 mSettings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 4 if(mSettings.getBoolean("isKeep", false)){ 5 etAccount.setText(mSettings.getString("username", "")); 6 etPass.setText(mSettings.getString("password", "")); 7 }else{ 8 etAccount.setText(""); 9 etPass.setText(""); 10 } 11 }
2.2.4 getData函数的引用要放到onCreate方法和onResume方法中
3. demo
https://github.com/cnfanhua/A-RemPass
标签:
原文地址:http://www.cnblogs.com/cnfanhua/p/4614658.html