今天做了什么:
完成了登录注册记住密码和自动登录的功能
package com.hui.testend;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.hui.testend.mytool.Dao;
import com.hui.testend.recongnize.baiduapi;
public class loginActivity extends AppCompatActivity implements View.OnClickListener{
EditText ed_name,ed_pass;
Button btn_login,btn_register;
Handler mainhander;
CheckBox remember_pass,remember_login;
Dao dao=new Dao();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
auto_login();
getap();
}
public void init(){
mainhander=new Handler(getMainLooper());
ed_name=findViewById(R.id.ed_name);
ed_pass=findViewById(R.id.ed_pass);
btn_login=findViewById(R.id.btn_login);
btn_register=findViewById(R.id.btn_register);
btn_login.setOnClickListener(this);
btn_register.setOnClickListener(this);
remember_pass=findViewById(R.id.cb_rember_pass);
remember_login=findViewById(R.id.cb_rember_login);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_login:
login();
break;
case R.id.btn_register:
register();
break;
}
}
public void register(){
Intent intent=new Intent(loginActivity.this,register.class);
startActivity(intent);
}
public void login(){
new Thread(new Runnable() {
@Override
public void run() {
String name=ed_name.getText().toString().trim();
String password=ed_pass.getText().toString().trim();
boolean key=dao.login(name,password);
mainhander.post(new Runnable() {
@Override
public void run() {
if (key){
setap(name,password);
Toast.makeText(loginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(loginActivity.this, MainActivity.class);
startActivity(intent);
loginActivity.this.finish();
}
else {
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
SharedPreferences.Editor edit=spf.edit();
edit.putBoolean("remember_pass",false);
Toast.makeText(loginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}).start();
}
public void setap(String name,String password){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
SharedPreferences.Editor edit=spf.edit();
edit.putString("name",name);
edit.putString("password",password);
if (remember_pass.isChecked()){
edit.putBoolean("remember_pass",true);
}
if (remember_login.isChecked()){
edit.putBoolean("remember_login",true);
}
edit.apply();
}
public void getap(){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
if(spf.getBoolean("remember_pass",false)){
ed_name.setText(spf.getString("name",""));
ed_pass.setText(spf.getString("password",""));
remember_pass.setChecked(true);
}
}
public void auto_login(){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
if (spf.getBoolean("remember_login",false)){
Intent intent=new Intent(loginActivity.this, MainActivity.class);
startActivity(intent);
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.hui.testend.mytool.Dao;
import com.hui.testend.recongnize.baiduapi;
public class loginActivity extends AppCompatActivity implements View.OnClickListener{
EditText ed_name,ed_pass;
Button btn_login,btn_register;
Handler mainhander;
CheckBox remember_pass,remember_login;
Dao dao=new Dao();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
auto_login();
getap();
}
public void init(){
mainhander=new Handler(getMainLooper());
ed_name=findViewById(R.id.ed_name);
ed_pass=findViewById(R.id.ed_pass);
btn_login=findViewById(R.id.btn_login);
btn_register=findViewById(R.id.btn_register);
btn_login.setOnClickListener(this);
btn_register.setOnClickListener(this);
remember_pass=findViewById(R.id.cb_rember_pass);
remember_login=findViewById(R.id.cb_rember_login);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_login:
login();
break;
case R.id.btn_register:
register();
break;
}
}
public void register(){
Intent intent=new Intent(loginActivity.this,register.class);
startActivity(intent);
}
public void login(){
new Thread(new Runnable() {
@Override
public void run() {
String name=ed_name.getText().toString().trim();
String password=ed_pass.getText().toString().trim();
boolean key=dao.login(name,password);
mainhander.post(new Runnable() {
@Override
public void run() {
if (key){
setap(name,password);
Toast.makeText(loginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(loginActivity.this, MainActivity.class);
startActivity(intent);
loginActivity.this.finish();
}
else {
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
SharedPreferences.Editor edit=spf.edit();
edit.putBoolean("remember_pass",false);
Toast.makeText(loginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}).start();
}
public void setap(String name,String password){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
SharedPreferences.Editor edit=spf.edit();
edit.putString("name",name);
edit.putString("password",password);
if (remember_pass.isChecked()){
edit.putBoolean("remember_pass",true);
}
if (remember_login.isChecked()){
edit.putBoolean("remember_login",true);
}
edit.apply();
}
public void getap(){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
if(spf.getBoolean("remember_pass",false)){
ed_name.setText(spf.getString("name",""));
ed_pass.setText(spf.getString("password",""));
remember_pass.setChecked(true);
}
}
public void auto_login(){
SharedPreferences spf=getSharedPreferences("Spf",MODE_PRIVATE);
if (spf.getBoolean("remember_login",false)){
Intent intent=new Intent(loginActivity.this, MainActivity.class);
startActivity(intent);
}
}
}
package com.hui.testend;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.hui.testend.mytool.Dao;
public class register extends AppCompatActivity implements View.OnClickListener {
EditText ed_name,ed_pass,ed_phone;
Button btn_sub_register;
Handler mainhander;
Dao dao=new Dao();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
init();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sub_resgiter:
register();
break;
}
}
public void register(){
new Thread(new Runnable() {
@Override
public void run() {
String name=ed_name.getText().toString().trim();
String password=ed_pass.getText().toString().trim();
String phone=ed_phone.getText().toString().trim();
int a=dao.register(name,password,phone);
mainhander.post(new Runnable() {
@Override
public void run() {
if (a==1){
Toast.makeText(register.this,"注册成功",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(register.this,"注册失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}).start();
}
public void init(){
mainhander=new Handler(getMainLooper());
ed_name=findViewById(R.id.ed_name);
ed_pass=findViewById(R.id.ed_pass);
ed_phone=findViewById(R.id.ed_phone);
btn_sub_register=findViewById(R.id.btn_sub_resgiter);
btn_sub_register.setOnClickListener(this);
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.hui.testend.mytool.Dao;
public class register extends AppCompatActivity implements View.OnClickListener {
EditText ed_name,ed_pass,ed_phone;
Button btn_sub_register;
Handler mainhander;
Dao dao=new Dao();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
init();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sub_resgiter:
register();
break;
}
}
public void register(){
new Thread(new Runnable() {
@Override
public void run() {
String name=ed_name.getText().toString().trim();
String password=ed_pass.getText().toString().trim();
String phone=ed_phone.getText().toString().trim();
int a=dao.register(name,password,phone);
mainhander.post(new Runnable() {
@Override
public void run() {
if (a==1){
Toast.makeText(register.this,"注册成功",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(register.this,"注册失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}).start();
}
public void init(){
mainhander=new Handler(getMainLooper());
ed_name=findViewById(R.id.ed_name);
ed_pass=findViewById(R.id.ed_pass);
ed_phone=findViewById(R.id.ed_phone);
btn_sub_register=findViewById(R.id.btn_sub_resgiter);
btn_sub_register.setOnClickListener(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".register">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="50sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:layout_gravity="center"
/>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"/>
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText
android:id="@+id/ed_pass"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"/>
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电 话:"/>
<EditText
android:id="@+id/ed_phone"
android:background="@drawable/ed_backgroud"
android:layout_width="match_parent"
android:layout_height="38dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="性别"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="女" />
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/btn_sub_resgiter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:background="@drawable/btn_bg_selector"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".register">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="50sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:layout_gravity="center"
/>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"/>
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText
android:id="@+id/ed_pass"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"/>
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电 话:"/>
<EditText
android:id="@+id/ed_phone"
android:background="@drawable/ed_backgroud"
android:layout_width="match_parent"
android:layout_height="38dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="性别"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="女" />
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/btn_sub_resgiter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:background="@drawable/btn_bg_selector"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_margin="10dp"
tools:context=".loginActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主界面"
android:textSize="50sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"
android:paddingRight="5dp"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20sp"
android:paddingRight="5dp"/>
<EditText
android:id="@+id/ed_pass"
android:layout_width="match_parent"
android:layout_height="38dp"
android:inputType="numberPassword"
android:background="@drawable/ed_backgroud"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="40dp">
<CheckBox
android:id="@+id/cb_rember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:layout_marginRight="120dp"/>
<CheckBox
android:id="@+id/cb_rember_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginRight="120dp"
android:layout_marginLeft="20dp"
android:background="@drawable/btn_bg_selector"
/>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20sp"
android:textStyle="bold"
android:background="@drawable/btn_bg_selector"/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_margin="10dp"
tools:context=".loginActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主界面"
android:textSize="50sp"
android:textStyle="bold"
android:textColor="@color/teal_700"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"
android:paddingRight="5dp"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/ed_backgroud"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20sp"
android:paddingRight="5dp"/>
<EditText
android:id="@+id/ed_pass"
android:layout_width="match_parent"
android:layout_height="38dp"
android:inputType="numberPassword"
android:background="@drawable/ed_backgroud"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="40dp">
<CheckBox
android:id="@+id/cb_rember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:layout_marginRight="120dp"/>
<CheckBox
android:id="@+id/cb_rember_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginRight="120dp"
android:layout_marginLeft="20dp"
android:background="@drawable/btn_bg_selector"
/>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20sp"
android:textStyle="bold"
android:background="@drawable/btn_bg_selector"/>
</LinearLayout>
</LinearLayout>
遇到了什么困难:
无
明天准备做什么:
继续完成团队作业
代码量 400行