Android 开发笔记___SQLite__优化记住密码功能
1 package com.example.alimjan.hello_world; 2 3 /** 4 * Created by alimjan on 7/4/2017. 5 */ 6 7 import com.example.alimjan.hello_world.bean.UserInfo; 8 import com.example.alimjan.hello_world.dataBase.UserDBHelper; 9 import com.example.alimjan.hello_world.Utils.DateUtil; 10 11 12 import android.app.AlertDialog; 13 import android.content.Context; 14 import android.content.DialogInterface; 15 import android.content.Intent; 16 import android.os.Bundle; 17 import android.support.v7.app.AppCompatActivity; 18 import android.text.Editable; 19 import android.text.TextWatcher; 20 import android.util.Log; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.View.OnFocusChangeListener; 24 import android.widget.AdapterView; 25 import android.widget.ArrayAdapter; 26 import android.widget.Button; 27 import android.widget.CheckBox; 28 import android.widget.CompoundButton; 29 import android.widget.EditText; 30 import android.widget.RadioButton; 31 import android.widget.RadioGroup; 32 import android.widget.Spinner; 33 import android.widget.TextView; 34 import android.widget.Toast; 35 import android.widget.AdapterView.OnItemSelectedListener; 36 37 38 public class class_4_2_3 extends AppCompatActivity implements OnClickListener, OnFocusChangeListener { 39 40 private RadioGroup rg_login; 41 private RadioButton rb_password; 42 private RadioButton rb_verifycode; 43 private EditText et_phone; 44 private TextView tv_password; 45 private EditText et_password; 46 private Button btn_forget; 47 private CheckBox ck_remember; 48 private Button btn_login; 49 50 private int mRequestCode = 0; 51 private int mType = 0; 52 private boolean bRemember = false; 53 private String mPassword = "111111"; 54 private String mVerifyCode; 55 private UserDBHelper mHelper; 56 57 @Override 58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 setContentView(R.layout.code_4_2_3); 61 rg_login = (RadioGroup) findViewById(R.id.rg_login); 62 rb_password = (RadioButton) findViewById(R.id.rb_password); 63 rb_verifycode = (RadioButton) findViewById(R.id.rb_verifycode); 64 et_phone = (EditText) findViewById(R.id.et_phone); 65 tv_password = (TextView) findViewById(R.id.tv_password); 66 et_password = (EditText) findViewById(R.id.et_password); 67 btn_forget = (Button) findViewById(R.id.btn_forget); 68 ck_remember = (CheckBox) findViewById(R.id.ck_remember); 69 btn_login = (Button) findViewById(R.id.btn_login); 70 71 rg_login.setOnCheckedChangeListener(new RadioListener()); 72 ck_remember.setOnCheckedChangeListener(new CheckListener()); 73 et_phone.addTextChangedListener(new HideTextWatcher(et_phone)); 74 et_password.addTextChangedListener(new HideTextWatcher(et_password)); 75 btn_forget.setOnClickListener(this); 76 btn_login.setOnClickListener(this); 77 et_password.setOnFocusChangeListener(this); 78 79 ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, 80 R.layout.item_select, typeArray); 81 typeAdapter.setDropDownViewResource(R.layout.item_dropdown); 82 Spinner sp_type = (Spinner) findViewById(R.id.sp_type); 83 sp_type.setPrompt("请选择用户类型"); 84 sp_type.setAdapter(typeAdapter); 85 sp_type.setSelection(mType); 86 sp_type.setOnItemSelectedListener(new TypeSelectedListener()); 87 } 88 89 private class RadioListener implements RadioGroup.OnCheckedChangeListener { 90 @Override 91 public void onCheckedChanged(RadioGroup group, int checkedId) { 92 if (checkedId == R.id.rb_password) { 93 tv_password.setText("登录密码:"); 94 et_password.setHint("请输入密码"); 95 btn_forget.setText("忘记密码"); 96 ck_remember.setVisibility(View.VISIBLE); 97 } else if (checkedId == R.id.rb_verifycode) { 98 tv_password.setText(" 验证码:"); 99 et_password.setHint("请输入验证码"); 100 btn_forget.setText("获取验证码"); 101 ck_remember.setVisibility(View.INVISIBLE); 102 } 103 } 104 } 105 106 private String[] typeArray = {"个人用户", "公司用户"}; 107 class TypeSelectedListener implements OnItemSelectedListener { 108 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 109 mType = arg2; 110 } 111 112 public void onNothingSelected(AdapterView<?> arg0) { 113 } 114 } 115 116 private class CheckListener implements CompoundButton.OnCheckedChangeListener { 117 @Override 118 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 119 if (buttonView.getId() == R.id.ck_remember) { 120 bRemember = isChecked; 121 } 122 } 123 } 124 125 private class HideTextWatcher implements TextWatcher { 126 private EditText mView; 127 private int mMaxLength; 128 private CharSequence mStr; 129 130 public HideTextWatcher(EditText v) { 131 super(); 132 mView = v; 133 mMaxLength = ViewUtil.getMaxLength(v); 134 } 135 136 @Override 137 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 138 } 139 140 @Override 141 public void onTextChanged(CharSequence s, int start, int before, int count) { 142 mStr = s; 143 } 144 145 @Override 146 public void afterTextChanged(Editable s) { 147 if (mStr == null || mStr.length() == 0) 148 return; 149 if ((mStr.length() == 11 && mMaxLength == 11) || 150 (mStr.length() == 6 && mMaxLength == 6)) { 151 ViewUtil.hideOneInputMethod(class_4_2_3.this, mView); 152 } 153 } 154 } 155 156 @Override 157 public void onClick(View v) { 158 String phone = et_phone.getText().toString(); 159 if (v.getId() == R.id.btn_forget) { 160 if (phone==null || phone.length()<11) { 161 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 162 return; 163 } 164 if (rb_password.isChecked() == true) { 165 Intent intent = new Intent(this, class_4_2_3_1.class); 166 intent.putExtra("phone", phone); 167 startActivityForResult(intent, mRequestCode); 168 } else if (rb_verifycode.isChecked() == true) { 169 mVerifyCode = String.format("%06d", (int)(Math.random()*1000000%1000000)); 170 AlertDialog.Builder builder = new AlertDialog.Builder(this); 171 builder.setTitle("请记住验证码"); 172 builder.setMessage("手机号"+phone+",本次验证码是"+mVerifyCode+",请输入验证码"); 173 builder.setPositiveButton("好的", null); 174 AlertDialog alert = builder.create(); 175 alert.show(); 176 } 177 } else if (v.getId() == R.id.btn_login) { 178 if (phone==null || phone.length()<11) { 179 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 180 return; 181 } 182 if (rb_password.isChecked() == true) { 183 if (et_password.getText().toString().equals(mPassword) != true) { 184 Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show(); 185 return; 186 } else { 187 loginSuccess(); 188 } 189 } else if (rb_verifycode.isChecked() == true) { 190 if (et_password.getText().toString().equals(mVerifyCode) != true) { 191 Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); 192 return; 193 } else { 194 loginSuccess(); 195 } 196 } 197 } 198 } 199 200 @Override 201 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 202 if (requestCode == mRequestCode && data!=null) { 203 //用户密码已改为新密码 204 mPassword = data.getStringExtra("new_password"); 205 } 206 } 207 208 //从修改密码页面返回登录页面,要清空密码的输入框 209 @Override 210 protected void onRestart() { 211 et_password.setText(""); 212 super.onRestart(); 213 } 214 215 @Override 216 protected void onResume() { 217 super.onResume(); 218 mHelper = UserDBHelper.getInstance(this, 2); 219 mHelper.openWriteLink(); 220 } 221 222 @Override 223 protected void onPause() { 224 super.onPause(); 225 mHelper.closeLink(); 226 } 227 228 private void loginSuccess() { 229 String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面", 230 et_phone.getText().toString(), typeArray[mType]); 231 AlertDialog.Builder builder = new AlertDialog.Builder(this); 232 builder.setTitle("登录成功"); 233 builder.setMessage(desc); 234 builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() { 235 @Override 236 public void onClick(DialogInterface dialog, int which) { 237 finish(); 238 } 239 }); 240 builder.setNegativeButton("我再看看", null); 241 AlertDialog alert = builder.create(); 242 alert.show(); 243 244 if (bRemember) { 245 UserInfo info = new UserInfo(); 246 info.phone = et_phone.getText().toString(); 247 info.password = et_password.getText().toString(); 248 info.update_time = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss"); 249 mHelper.insert(info); 250 } 251 } 252 253 //为什么光标进入密码框事件不选onClick?因为要点两下才会触发onClick动作(第一下是切换焦点动作) 254 @Override 255 public void onFocusChange(View v, boolean hasFocus) { 256 String phone = et_phone.getText().toString(); 257 if (v.getId() == R.id.et_password) { 258 if (phone.length() > 0 && hasFocus == true) { 259 UserInfo info = mHelper.queryByPhone(phone); 260 if (info != null) { 261 et_password.setText(info.password); 262 }else{ 263 et_password.setText(""); 264 } 265 } 266 } 267 } 268 269 public static void startHome(Context mContext) { 270 Intent intent = new Intent(mContext, class_4_2_3.class); 271 mContext.startActivity(intent); 272 } 273 274 275 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:focusable="true" 5 android:focusableInTouchMode="true" 6 android:orientation="vertical" 7 android:padding="5dp" > 8 9 <RadioGroup 10 android:id="@+id/rg_login" 11 android:layout_width="match_parent" 12 android:layout_height="60dp" 13 android:orientation="horizontal" > 14 15 <RadioButton 16 android:id="@+id/rb_password" 17 android:layout_width="0dp" 18 android:layout_height="match_parent" 19 android:layout_weight="1" 20 android:checked="true" 21 android:gravity="left|center" 22 android:text="密码登录" 23 android:textColor="@color/black" 24 android:textSize="17sp" /> 25 26 <RadioButton 27 android:id="@+id/rb_verifycode" 28 android:layout_width="0dp" 29 android:layout_height="match_parent" 30 android:layout_weight="1" 31 android:checked="false" 32 android:gravity="left|center" 33 android:text="验证码登录" 34 android:textColor="@color/black" 35 android:textSize="17sp" /> 36 </RadioGroup> 37 38 <RelativeLayout 39 android:layout_width="match_parent" 40 android:layout_height="60dp" > 41 42 <TextView 43 android:id="@+id/tv_type" 44 android:layout_width="wrap_content" 45 android:layout_height="match_parent" 46 android:layout_alignParentLeft="true" 47 android:gravity="center" 48 android:text=" 我是:" 49 android:textColor="@color/black" 50 android:textSize="17sp" /> 51 52 <Spinner 53 android:id="@+id/sp_type" 54 android:layout_width="match_parent" 55 android:layout_height="match_parent" 56 android:layout_toRightOf="@+id/tv_type" 57 android:gravity="left|center" 58 android:spinnerMode="dialog" /> 59 </RelativeLayout> 60 61 <RelativeLayout 62 android:layout_width="match_parent" 63 android:layout_height="60dp" > 64 65 <TextView 66 android:id="@+id/tv_phone" 67 android:layout_width="wrap_content" 68 android:layout_height="match_parent" 69 android:layout_alignParentLeft="true" 70 android:gravity="center" 71 android:text="手机号码:" 72 android:textColor="@color/black" 73 android:textSize="17sp" /> 74 75 <EditText 76 android:id="@+id/et_phone" 77 android:layout_width="match_parent" 78 android:layout_height="match_parent" 79 android:layout_marginBottom="5dp" 80 android:layout_marginTop="5dp" 81 android:layout_toRightOf="@+id/tv_phone" 82 android:background="@drawable/editext_selector" 83 android:gravity="left|center" 84 android:hint="请输入手机号码" 85 android:inputType="number" 86 android:maxLength="11" 87 android:textColor="@color/black" 88 android:textColorHint="@color/grey" 89 android:textCursorDrawable="@drawable/text_cursor" 90 android:textSize="17sp" /> 91 </RelativeLayout> 92 93 <RelativeLayout 94 android:layout_width="match_parent" 95 android:layout_height="60dp" > 96 97 <TextView 98 android:id="@+id/tv_password" 99 android:layout_width="wrap_content" 100 android:layout_height="match_parent" 101 android:layout_alignParentLeft="true" 102 android:gravity="center" 103 android:text="登录密码:" 104 android:textColor="@color/black" 105 android:textSize="17sp" /> 106 107 <FrameLayout 108 android:layout_width="match_parent" 109 android:layout_height="match_parent" 110 android:layout_toRightOf="@+id/tv_password" > 111 112 <EditText 113 android:id="@+id/et_password" 114 android:layout_width="match_parent" 115 android:layout_height="match_parent" 116 android:layout_marginBottom="5dp" 117 android:layout_marginTop="5dp" 118 android:background="@drawable/editext_selector" 119 android:gravity="left|center" 120 android:hint="请输入密码" 121 android:inputType="numberPassword" 122 android:maxLength="6" 123 android:textColor="@color/black" 124 android:textColorHint="@color/grey" 125 android:textCursorDrawable="@drawable/text_cursor" 126 android:textSize="17sp" /> 127 128 <Button 129 android:id="@+id/btn_forget" 130 android:layout_width="wrap_content" 131 android:layout_height="match_parent" 132 android:layout_gravity="right" 133 android:gravity="center" 134 android:text="忘记密码" 135 android:textColor="@color/black" 136 android:textSize="17sp" /> 137 </FrameLayout> 138 </RelativeLayout> 139 140 <CheckBox 141 android:id="@+id/ck_remember" 142 android:layout_width="match_parent" 143 android:layout_height="wrap_content" 144 android:button="@drawable/checkbox_selector" 145 android:checked="false" 146 android:padding="10dp" 147 android:text="记住密码" 148 android:textColor="@color/black" 149 android:textSize="17sp" /> 150 151 <Button 152 android:id="@+id/btn_login" 153 android:layout_width="match_parent" 154 android:layout_height="wrap_content" 155 android:text="登录" 156 android:textColor="@color/black" 157 android:textSize="22sp" /> 158 159 </LinearLayout>
1 package com.example.alimjan.hello_world; 2 3 /** 4 * Created by alimjan on 7/4/2017. 5 */ 6 7 8 import android.app.Activity; 9 import android.app.AlertDialog; 10 import android.content.Context; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.support.v7.app.AppCompatActivity; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.EditText; 17 import android.widget.Toast; 18 19 public class class_4_2_3_1 extends AppCompatActivity implements OnClickListener { 20 21 private EditText et_password_first; 22 private EditText et_password_second; 23 private EditText et_verifycode; 24 private String mVerifyCode; 25 private String mPhone; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.code_4_2_3_1); 31 et_password_first = (EditText) findViewById(R.id.et_password_first); 32 et_password_second = (EditText) findViewById(R.id.et_password_second); 33 et_verifycode = (EditText) findViewById(R.id.et_verifycode); 34 findViewById(R.id.btn_verifycode).setOnClickListener(this); 35 findViewById(R.id.btn_confirm).setOnClickListener(this); 36 mPhone = getIntent().getStringExtra("phone"); 37 } 38 39 @Override 40 public void onClick(View v) { 41 if (v.getId() == R.id.btn_verifycode) { 42 if (mPhone==null || mPhone.length()<11) { 43 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 44 return; 45 } 46 mVerifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000)); 47 AlertDialog.Builder builder = new AlertDialog.Builder(this); 48 builder.setTitle("请记住验证码"); 49 builder.setMessage("手机号"+mPhone+",本次验证码是"+mVerifyCode+",请输入验证码"); 50 builder.setPositiveButton("好的", null); 51 AlertDialog alert = builder.create(); 52 alert.show(); 53 } else if (v.getId() == R.id.btn_confirm) { 54 String password_first = et_password_first.getText().toString(); 55 String password_second = et_password_second.getText().toString(); 56 if (password_first==null || password_first.length()<6 || 57 password_second==null || password_second.length()<6) { 58 Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show(); 59 return; 60 } 61 if (password_first.equals(password_second) != true) { 62 Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show(); 63 return; 64 } 65 if (et_verifycode.getText().toString().equals(mVerifyCode) != true) { 66 Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); 67 return; 68 } else { 69 Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show(); 70 Intent intent = new Intent(); 71 intent.putExtra("new_password", password_first); 72 setResult(Activity.RESULT_OK, intent); 73 finish(); 74 } 75 } 76 } 77 78 public static void startHome(Context mContext) { 79 Intent intent = new Intent(mContext, class_4_2_3_1.class); 80 mContext.startActivity(intent); 81 } 82 83 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:focusable="true" 5 android:focusableInTouchMode="true" 6 android:orientation="vertical" 7 android:padding="5dp" > 8 9 <RelativeLayout 10 android:layout_width="match_parent" 11 android:layout_height="60dp" > 12 13 <TextView 14 android:id="@+id/tv_password_first" 15 android:layout_width="wrap_content" 16 android:layout_height="match_parent" 17 android:layout_alignParentLeft="true" 18 android:gravity="center" 19 android:text="输入新密码:" 20 android:textColor="@color/black" 21 android:textSize="17sp" /> 22 23 <EditText 24 android:id="@+id/et_password_first" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:layout_marginBottom="5dp" 28 android:layout_marginTop="5dp" 29 android:layout_toRightOf="@+id/tv_password_first" 30 android:background="@drawable/editext_selector" 31 android:gravity="left|center" 32 android:hint="请输入新密码" 33 android:inputType="numberPassword" 34 android:maxLength="11" 35 android:textColor="@color/black" 36 android:textColorHint="@color/grey" 37 android:textCursorDrawable="@drawable/text_cursor" 38 android:textSize="17sp" /> 39 </RelativeLayout> 40 41 <RelativeLayout 42 android:layout_width="match_parent" 43 android:layout_height="60dp" > 44 45 <TextView 46 android:id="@+id/tv_password_second" 47 android:layout_width="wrap_content" 48 android:layout_height="match_parent" 49 android:layout_alignParentLeft="true" 50 android:gravity="center" 51 android:text="确认新密码:" 52 android:textColor="@color/black" 53 android:textSize="17sp" /> 54 55 <EditText 56 android:id="@+id/et_password_second" 57 android:layout_width="match_parent" 58 android:layout_height="match_parent" 59 android:layout_marginBottom="5dp" 60 android:layout_marginTop="5dp" 61 android:layout_toRightOf="@+id/tv_password_second" 62 android:background="@drawable/editext_selector" 63 android:gravity="left|center" 64 android:hint="请再次输入新密码" 65 android:inputType="numberPassword" 66 android:maxLength="11" 67 android:textColor="@color/black" 68 android:textColorHint="@color/grey" 69 android:textCursorDrawable="@drawable/text_cursor" 70 android:textSize="17sp" /> 71 </RelativeLayout> 72 73 <RelativeLayout 74 android:layout_width="match_parent" 75 android:layout_height="60dp" > 76 77 <TextView 78 android:id="@+id/tv_verifycode" 79 android:layout_width="wrap_content" 80 android:layout_height="match_parent" 81 android:layout_alignParentLeft="true" 82 android:gravity="center" 83 android:text=" 验证码:" 84 android:textColor="@color/black" 85 android:textSize="17sp" /> 86 87 <FrameLayout 88 android:layout_width="match_parent" 89 android:layout_height="match_parent" 90 android:layout_toRightOf="@+id/tv_verifycode" > 91 92 <EditText 93 android:id="@+id/et_verifycode" 94 android:layout_width="match_parent" 95 android:layout_height="match_parent" 96 android:layout_marginBottom="5dp" 97 android:layout_marginTop="5dp" 98 android:background="@drawable/editext_selector" 99 android:gravity="left|center" 100 android:hint="请输入验证码" 101 android:inputType="numberPassword" 102 android:maxLength="6" 103 android:textColor="@color/black" 104 android:textColorHint="@color/grey" 105 android:textCursorDrawable="@drawable/text_cursor" 106 android:textSize="17sp" /> 107 108 <Button 109 android:id="@+id/btn_verifycode" 110 android:layout_width="wrap_content" 111 android:layout_height="match_parent" 112 android:layout_gravity="right" 113 android:gravity="center" 114 android:text="获取验证码" 115 android:textColor="@color/black" 116 android:textSize="17sp" /> 117 </FrameLayout> 118 </RelativeLayout> 119 120 <Button 121 android:id="@+id/btn_confirm" 122 android:layout_width="match_parent" 123 android:layout_height="wrap_content" 124 android:text="确定" 125 android:textColor="@color/black" 126 android:textSize="22sp" /> 127 128 </LinearLayout>
当输入完手机号之后,点击密码编辑框时,从数据库查看内容,如果含有该号的密码则自动添加,如果没有则空。勾选记住密码选项之后,如果登陆成功则保存到数据库。