制作一个简单的登陆页面
实现目的
1.原因
有很久没有写文章了,刚好最近在学习Android应用开发,所以想把自己最近的心得体会写一下。
这个登录页面由于自己之前对于Android应用开发的部分错误认识,导致自己走了一小段时间错误的路,不过想明白之后倒是觉得豁然开朗。
2.实现功能
一个简单的登陆页面:
- 用户名和密码的输入。
- 选中记住密码的复选框选项时会记住密码。
- 记住密码后会在登录页面自动填充用户名密码。
- 不选中记住密码复选框时会不保存密码,或者清空已经保存的密码。
- 登录按钮成功的跳转。
- .用户名,密码错误多次输入之后销毁活动。
实现截图
1.登陆页面
2.正确输入账户密码且记住密码
之后重新进入登陆页面,会自动填充保存的正确的账户密码。
3.如果账户密码输入正确,但不选择记住账户密码
账户密码输入正确,但不选择记住账户密码,也会登陆成功,但是会清空之前保存的账户密码。再次进入登陆页面时,需要重新输入账户密码,选择是否记住密码。
4.账户密码不正确
账户密码不正确累计达到10次的话,会销毁活动。
实现代码
1.登录页面 title.java:
package com.example.ning_eleven;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class title1 extends AppCompatActivity {
private final String the = "the", tao = "tao"; //后台的用户名和密码
private String account, password; //输入的用户名和密码
private int count = 0; //用来判断错误次数
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title1);
EditText accountET = findViewById(R.id.account); //获取控件
EditText passwordET = findViewById(R.id.password);
CheckBox remember = findViewById(R.id.checkbox);
Button login = findViewById(R.id.login);
SharedPreferences sp = getSharedPreferences("tao", MODE_PRIVATE);
//获得SharedPreferences,并创建文件名为tao
SharedPreferences.Editor editor = sp.edit();
//获得Editor对象,用于储存用户信息
if(sp.getString("account",account) !=null
&& sp.getString("password",password) !=null){
accountET.setText(sp.getString("account",account));
passwordET.setText(sp.getString("password",password));
}
//如果之前记住过密码,直接先导入。
login.setOnClickListener(new View.OnClickListener() { //点击事件监听器
@Override
public void onClick(View view) {
account = accountET.getText().toString();
password = passwordET.getText().toString();
if (account.equals(the) && password.equals(tao)){
if(remember.isChecked()){
editor.putString("account",account);
editor.putString("password",password);
editor.commit();
Intent intent = new Intent(title1.this,title1_1.class);
Toast.makeText(title1.this, "登陆成功,已记住密码", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
else {
editor.clear().commit(); //之前保存的账户密码进行清空
Intent intent = new Intent(title1.this,title1_1.class);
Toast.makeText(title1.this, "登陆成功", Toast.LENGTH_SHORT).show();
startActivity(intent);
finish();
//防止通过返回键再次进入登陆页面,此时登陆页面依然保留账户密码。
//所以销毁活动,再次进入登陆页面时,需要重新输入账号密码。
}
}
else {
count +=1;
Toast.makeText(title1.this, "用户名或密码错误,登陆失败,已错误"+count+"次", Toast.LENGTH_SHORT).show();
if (count ==10){
Toast.makeText(title1.this, "账户密码输入错误累计10次,已退出活动", Toast.LENGTH_SHORT).show();
finish();
}
}
}
});
}
}
2.登陆页面布局文件 activity_title1.xml:
<?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="@drawable/img2"
>
<LinearLayout
android:layout_marginTop="150dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账户:"
android:textColor="#0000ff"
/>
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#0000ff"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textColor="@color/black"
/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/black"
android:inputType="textPassword"
/>
</LinearLayout>
<CheckBox
android:id="@+id/checkbox"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textColor="#66dd6d"
/>
<Button
android:id="@+id/login"
android:layout_margin="30dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#66dd6d"
/>
</LinearLayout>
3.跳转页面,跳转页面布局,以及背景图片就不放上来了。
总结
最终写出的效果与老师所给出的要求有所区别,不过确实慢慢达到了自己最初的预期效果。希望自己下一次写代码的速度可以更快,思维跳转的也更灵敏,同时也能够做出更好的效果。