24.2.12
活动:LoginActivity.java
public class LoginActivity extends Activity implements View.OnClickListener { private static final int REQUEST_CODE_GO_TO_REGIST = 100; private EditText edit_account, edit_password; private Button btn_signIn, btn_signUp; private String account, password; private CheckBox box_rememberpsw; private DBHelper dbHelper = new DBHelper(this); private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; private static final long DELAY_TIME = 2000L; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login_layout); init(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CODE_GO_TO_REGIST: if (resultCode == RESULT_OK) { String name = data.getStringExtra("Phone"); String psw = data.getStringExtra("Password"); edit_account.setText(name); edit_password.setText(psw); } break; } } private void init() { edit_account = (EditText) findViewById(R.id.phone_text); edit_account.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { return false; } }); edit_password = (EditText) findViewById(R.id.password_text); box_rememberpsw = (CheckBox) findViewById(R.id.remember_pass); box_rememberpsw = (CheckBox) findViewById(R.id.remember_pass); btn_signIn = (Button) findViewById(R.id.signIn_btn); btn_signUp = (Button) findViewById(R.id.signUp_btn); btn_signIn.setOnClickListener(this); btn_signUp.setOnClickListener(this); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); boolean isRemember = sharedPreferences.getBoolean("remember_password", false); if (isRemember) { String name = sharedPreferences.getString("Account", ""); String psw = sharedPreferences.getString("Password", ""); edit_account.setText(name); edit_password.setText(psw); box_rememberpsw.setChecked(true); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.signUp_btn: Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivityForResult(intent, REQUEST_CODE_GO_TO_REGIST); break; case R.id.signIn_btn: if (edit_account.getText().toString().equals("") || edit_password.getText().toString().trim().equals("")) { Toast.makeText(LoginActivity.this, "手机号或密码不能为空!", Toast.LENGTH_SHORT).show(); } else { readUserInfo(); } break; } } private void readUserInfo() { account = edit_account.getText().toString(); password = edit_password.getText().toString().trim(); editor = sharedPreferences.edit(); if (login(account, password)) { if (box_rememberpsw.isChecked()) { editor.putBoolean("remember_password", true); editor.putString("Account", account); editor.putString("Password", password); } else { editor.clear(); } editor.apply(); Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); intent.putExtra("UserPhone", account); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "手机号或密码错误,请重新输入!", Toast.LENGTH_SHORT).show(); } } private boolean login(String account, String password) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = "select * from " + USER_TABLE + " where " + PHONE + "=? and " + PASSWORD + "=?"; Cursor cursor = db.rawQuery(sql, new String[]{account, password}); if (cursor.moveToFirst()) { cursor.close(); return true; } return false; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)