完成登录注册界面的设计以及相关java文件的代码,但仍存在一些问题
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#4169E1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_gravity="center" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#000000" android:textSize="55sp" android:textStyle="bold" android:text="福星会议" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:src="@drawable/huiyi"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="用户:" android:drawableLeft="@drawable/yonghu" android:textColor="#000000" android:textSize="28dp" /> <EditText android:layout_width="250dp" android:layout_height="wrap_content" android:id="@+id/username" android:layout_marginTop="50dp" android:layout_marginLeft="5dp" android:background="@drawable/bg_username" android:maxLines="1" android:singleLine="true" android:textSize="28dp" android:hint="请输入用户名" android:paddingLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:drawableLeft="@drawable/mima" android:textColor="#000000" android:textSize="28dp" /> <EditText android:layout_width="250dp" android:layout_height="wrap_content" android:id="@+id/password" android:background="@drawable/bg_username" android:layout_marginLeft="5dp" android:maxLines="1" android:textSize="28dp" android:hint="请输入密码" android:singleLine="true" android:paddingLeft="10dp" android:inputType="textWebPassword" />//隐藏密码 </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkbox_zhanghao" android:orientation="vertical" android:layout_marginLeft="5dp" android:checked="false" android:textSize="20dp" android:text="自动登录" android:textColor="#000000" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkbox_text" android:orientation="vertical" android:layout_marginLeft="5dp" android:checked="false" android:textSize="20dp" android:text="记住密码" android:textColor="#000000" /> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:background="@drawable/bg_btn_1" android:paddingLeft="40dp" android:paddingRight="40dp" android:text="登录" android:textSize="30dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" android:paddingTop="40dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码?" android:layout_marginLeft="25dp" android:textSize="30sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_register" android:layout_marginLeft="70dp" android:paddingLeft="40dp" android:paddingRight="40dp" android:textSize="30sp" android:text="注册" /> </LinearLayout> </LinearLayout>
package com.example.fuxing; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.fuxing.service.UserService; import java.util.Map; public class LoginActivity extends AppCompatActivity { private CheckBox rem_pw; private CheckBox auto_login; private EditText username; private EditText password; private Button login; private TextView register; private SharedPreferences sp; @SuppressLint("WrongConstant") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_login );//即activity_login.xml sp = this.getSharedPreferences( "userInfo", Context.MODE_PRIVATE ); username = findViewById( R.id.username ); password = findViewById( R.id.password ); login = findViewById( R.id.btn_login ); register = findViewById( R.id.btn_register ); rem_pw = findViewById( R.id.checkbox_text ); auto_login = findViewById( R.id.checkbox_zhanghao ); /*Map<String, String> map=User.Get(this); if(map!=null){ username.setText(map.get("user")); password.setText(map.get("key")); }*/ //判断记住密码多选框的状态 if(sp.getBoolean("ISCHECK", true)) { //设置默认是记录密码状态 rem_pw.setChecked(true); username.setText(sp.getString("USER_NAME", "")); password.setText(sp.getString("PASSWORD", "")); //判断自动登陆多选框状态 if(sp.getBoolean("AUTO_ISCHECK", true)) { //设置默认是自动登录状态 auto_login.setChecked(true); //跳转界面 Intent intent = new Intent(LoginActivity.this,MainActivity.class); LoginActivity.this.startActivity(intent); } } //登录事件 login.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String name = username.getText().toString(); System.out.println( name ); String pass = password.getText().toString(); System.out.println( pass ); Log.i( "TAG", name + "_" + pass ); UserService uService = new UserService( LoginActivity.this ); boolean flag = uService.login( name, pass ); if (flag) { Log.i( "TAG", "登录成功" ); Toast.makeText( LoginActivity.this, "登录成功", Toast.LENGTH_LONG ).show(); //登录成功和记住密码框为选中状态才保存用户信息 if(rem_pw.isChecked()) { //记住用户名、密码、 SharedPreferences.Editor editor = sp.edit(); editor.putString("USER_NAME", name); editor.putString("PASSWORD", pass); editor.commit(); } //跳转界面 Intent intent = new Intent( LoginActivity.this, MainActivity.class ); startActivity( intent ); } else { Log.i( "TAG", "登录失败" ); Toast.makeText( LoginActivity.this, "登录失败", Toast.LENGTH_LONG ).show(); } } } ); rem_pw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (rem_pw.isChecked()) { System.out.println("记住密码已选中"); sp.edit().putBoolean("ISCHECK", true).commit(); }else { System.out.println("记住密码没有选中"); sp.edit().putBoolean("ISCHECK", false).commit(); } } }); //监听自动登录多选框事件 auto_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (auto_login.isChecked()) { System.out.println("自动登录已选中"); sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); } else { System.out.println("自动登录没有选中"); sp.edit().putBoolean("AUTO_ISCHECK", false).commit(); } } }); //注册事件 register.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent( LoginActivity.this, RegisterActivity.class ); startActivity( intent ); } } ); } }