效果图
填入正确的用户名和密码
跳转到登录成功界面
填入错误的用户名和密码
提示用户名和密码不符
开始实现
要求:
编写一个登录界面和一个登录成功界面, 结合自己的登录界面中的输入内容,将其用SharedPreferences方式存储,并且在登录的时候做一下逻辑判断进行数据比较,只有数据一致之后才能跳转到登录成功界面,否则做相应的错误提示
登录页面布局
根据需求设计登录界面activity_main.xml,这里用到了两个ImageView、两个EditText和一个Button按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/back" tools:context=".MainActivity">
<EditText android:id="@+id/usr" android:layout_width="250dp" android:layout_height="40dp" android:layout_marginTop="60dp" android:background="@null" android:hint="请输入用户名" android:imeOptions="actionSend" android:inputType="text" android:padding="5dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.38" app:layout_constraintStart_toEndOf="@+id/imageView3" app:layout_constraintTop_toTopOf="parent" />
<EditText android:id="@+id/pwd" android:layout_width="250dp" android:layout_height="40dp" android:layout_marginTop="10dp" android:background="@null" android:hint="请输入密码" android:imeOptions="actionSend" android:inputType="text" android:padding="5dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.38" app:layout_constraintStart_toEndOf="@+id/imageView4" app:layout_constraintTop_toBottomOf="@+id/usr" />
<Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="44dp" android:text="登录" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pwd" />
<ImageView android:id="@+id/imageView3" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="30dp" android:layout_marginTop="60dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/usr" />
<ImageView android:id="@+id/imageView4" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="30dp" android:layout_marginTop="10dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView3" app:srcCompat="@drawable/pwd" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
数据判断能否登录
在MainActivity中先将正确的用户名和密码用SharedPreferences方式存储,再和自己的登录界面中的输入内容做一下逻辑判断进行数据比较
如果符合,跳转到登录成功界面,否则提示错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.huatec.edu.hw8avtivity;
import androidx.appcompat.app.AppCompatActivity;
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.EditText; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends AppCompatActivity { EditText usr; EditText pwd; Button login;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
usr = findViewById(R.id.usr); pwd = findViewById(R.id.pwd); login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("user_name","tian"); editor.putString("pass_word","18"); editor.apply(); String user_name = sharedPreferences.getString("user_name",null); String pass_word = sharedPreferences.getString("pass_word",null);
String username = usr.getText().toString(); String password = pwd.getText().toString(); if(user_name.equals(username) && pass_word.equals(password)) { Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } else { Toast.makeText(getApplicationContext(),"用户名或密码不符",Toast.LENGTH_LONG).show(); } } });
} }
|
登录成功界面
activity_main2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".Main2Activity">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录成功" android:textSize="25sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
|
Main2Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.huatec.edu.hw8avtivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } }
|