Android Minitwitter 记住密码功能
MiniTwitter记住密码功能实现
首先,在进入本次主要内容之前说一下,本功能的实现是在twitter登陆界面的基础上操作,但本次主要任务内容是记住密码的功能实现,所以登陆界面不在详细介绍。
如图:为本次实验的结果图;

1、界面介绍
布局构造:布局分为三大部分
(1)背景:使用LinearLayout布局;
(2)浅蓝色部分:使用RelativeLayout布局;
注意:这里用到圆角设置corners和填充色设置solid;
(3)输入框和按钮:使用TextView、EditText 、Button;
aivity_main.xml代码如下 :
1 <!-- 用户名 --> 2 3 <LinearLayout 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:layout_margin="10dp" 7 android:orientation="horizontal"> 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_weight="1" 12 android:layout_marginRight="5dp" 13 android:text="@string/tv_username" 14 /> 15 16 <EditText 17 android:id="@+id/et_username" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_weight="3" 21 /> 22 </LinearLayout〉 23 <!-- 密码 --> 24 <LinearLayout 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_margin="10dp" 28 android:orientation="horizontal"> 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_weight="1" 33 android:layout_marginRight="5dp" 34 android:text="@string/tv_password" 35 /> 36 <EditText 37 android:id="@+id/et_password" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_weight="3" 41 android:password="true" 42 /> 43 </LinearLayout〉 44 45 <!-- 按钮 --> 46 <LinearLayout 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:layout_marginTop="5dp" 50 android:layout_marginRight="10dp" 51 android:layout_marginLeft="10dp" 52 android:orientation="horizontal"> 53 <CheckBox 54 android:id:="@+id/cb_keeppsd" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:layout_weight="1" 58 android:text="@string/tv_password" 59 android:checked="true" 60 /> 61 <Button 62 android:id="@+id/btn_login" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:layout_weight="3" 66 android:layout_marginLeft="20dp" 67 android:text="@string/btn_login" /> 68 </LinearLayout〉 69 </LinearLayout〉
其中与原登录界面不同的是,添加了一个CheckBox控件,当用户勾选了此对话框并点击登录按扭时,可以记录用户的名字和密 码。
(4)该activity是无标题的,所以要在setContentView;
2、记住密码功能实现
(1)用SharedPreferences来储存密码
代码如下:
1 private SharedPreferences setting = null; 2 private static final String PREFS_NAME = "NamePwd"; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 findView(); 9 setListener() ; 10 getData(); 11 }
(2)监听控件
代码如下:
1 private void setListener() { 2 // 为登录按钮绑定事件 3 btnLogin.setOnClickListener(new OnClickListener() { 4 @Override 5 public void onClick(View arg0) { 6 7 // 判断用户名和密码 8 if ("admin".equals(mEtUserName.getText().tostring())&&"123456".equals(mEtPassWord.getText().toString())) 9 {
PS:判断用户名和密码的合法性
(3) MainActivity
代码如下:
1 public class MainActivity extends Activity { 2 3 private EditText etUsername; 4 private EditText etPassword; 5 private CheckBox rememberpassword; 6 private Button btnLogin; 7 8 // 声明一个SharedPreferences用于保存数据 9 private SharedPreferences setting = null; 10 private static final String PREFS_NAME = "NamePwd"; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 findView(); 17 setListener() ; 18 getData(); 19 } 20 21 private void findView() { 22 metUsername=(EditText)findViewById(R.id.et_Username); 23 metPassword=(EditText)findViewById(R.id.et_Password); 24 mCBKeepPsd=(CheckBox)findViewById(R.id.cb_keeppsd); 25 mbtnLogin=(Button)findViewById(R.id.btn_Login); 26 } 27 private void setListener() { 28 // 为登录按钮绑定事件 29 btnLogin.setOnClickListener(new OnClickListener() { 30 @Override 31 public void onClick(View arg0) { 32 33 // 判断用户名和密码 34 if ("admin".equals(mEtUserName.getText().tostring()) 35 &&"123456".equals(mEtPassWord.getText() 36 .toString())) 37 { 38 // 判断复选框是否选中 39 if (mcbKeepPsd.isChecked()) 40 { 41 setting = getSharedPreferences(PREFS_NAME, 42 MODE_PRIVATE); 43 // 得到Editor对象 44 Editor edit = mspSettings.edit(); 45 // 记录保存标记 46 edit.putBoolean("iskeep", true); 47 // 记录用户名 48 edit.putString("username",metusername.getText() 49 .toString()); 50 // 记录密码 51 edit.putString("password",metPassword.getText() 52 .toString()); 53 edit.commit(); 54 } 55 56 else 57 { 58 mspSettings=getSharedPreferences(PREFS_NAME, 59 MODE_PRIVATE); 60 // 得到Editor对象 61 Editor edit = mspSettings.edit(); 62 // 记录保存标记 63 edit.putBoolean("iskeep", false); 64 // 记录用户名 65 edit.putString("username", ""); 66 // 记录密码 67 edit.putString("password", ""); 68 edit.commit(); 69 } 70 */ 71 72 // 跳转到首页 73 Intent intent = new Intent(MainActivitythis, 74 successActivity.class); 75 startActivity(intent); 76 finish(); 77 } 78 } 79 else 80 { 81 // 显示错误提示 82 Toast.makeText(getApplicationContext(), "用户名或密码错误", 83 Toast.LENGTH_SHORT).show(); 84 } 85 86 } 87 }); 88 } 89 90 @Override 91 protected void onResume() { 92 // 在界面显示数据之前得到之前存储的数据 93 super.onResume(); 94 getData(); 95 } 96 private void getData() { 97 // 得到sharedpreferences对象 98 setting = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 99 // 判断是否之前存储过用户名密码 100 if (setting.getBoolean("isKeep", false)) { 101 // 如果之前存储过,则显示在相应文本框内 102 etUsername.setText(setting.getString("username", "")); 103 etPassword.setText(setting.getString("password", "")); 104 } else { 105 // 否则显示空 106 metUsername.setText(""); 107 metPassword.setText(""); 108 } 109 } 110 111 }
(4)出现用户名密码输入错误情况下,清空记录并跳转
代码如下:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.success_main);
1 <TextView 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:layout_centerInparent="true" 5 android:layout_textSize="20sp" 6 android:text="@string/tv_success" 7 />

浙公网安备 33010602011771号