想要制作一个简易的登录界面非常容易,总体上来说就是UI布局、给定id、新建跳转的页面、以及输入账号密码的获取与判断,那么接下来就开始制作吧!

1.首先就是Activity中的组件布局,这个就不一一列举了!自己把两个EditText和一个Button摆好就ok了,像按钮的点击效果可以自己设计一下(默认状态是什么颜色,按下去是什么颜色)。

2.再一个就是要给定控件一个id
<?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:background="@drawable/img_1" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="160dp" android:orientation="vertical" android:padding="30dp" android:gravity="center"> <EditText android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/EDit_username" android:hint="账户名" android:maxLines="1" android:textColor="#000000"/> <EditText android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/EDit_password" android:layout_marginTop="15dp" android:hint="账户名" android:maxLines="1" android:textColor="#000000"/> <Button android:layout_width="200dp" android:layout_height="60dp" android:layout_marginTop="30dp" android:id="@+id/btn_login" android:text="登录" android:backgroundTint="@color/btn_xiaoguo" android:textSize="20sp"/> </LinearLayout> </LinearLayout> </LinearLayout>
3.然后就是要在Mainactivity.java中写代码了,需要申明控件id,绑定控件id及登录按钮的点击事件(判断是否是自己设定的密码,判断是否达到一定的长度)。 对了,还有需要定义存账号密码的类型名称。
package com.example.denlu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mEDit_password;
private EditText mEDit_username;
private Button mbtn_login;
private String zhanhao; //申明存入账号的变量
private String mima; //申明存入密码的变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEDit_username = findViewById(R.id.EDit_username); //绑定账号Edit Text的id
mEDit_password = findViewById(R.id.EDit_password); //绑定密码Edit Text的id
mbtn_login = findViewById(R.id.btn_login); //绑定按钮Button的id
4.好了,现在要做的就是写按钮的点击事件了;那么在这之前需要先新建一个跳转之后的界面。之前也发过新建一个Activity的方法。

5.然后写点击事件;那么点击事件要怎么写,首先肯定是要把账号与密码都提取出来存入自定义的String变量,需要用到 .getText().toString() 这两个函数;既然提取出来了那么下一步就好办了,直接用几个if else if 写几个判断即可。
package com.example.denlu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mEDit_password;
private EditText mEDit_username;
private Button mbtn_login;
private String zhanghao; //申明存入账号的变量
private String mima; //申明存入密码的变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEDit_username = findViewById(R.id.EDit_username); //绑定账号Edit Text的id
mEDit_password = findViewById(R.id.EDit_password); //绑定密码Edit Text的id
mbtn_login = findViewById(R.id.btn_login); //绑定按钮Button的id
mbtn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
zhanghao = mEDit_username.getText().toString(); //将账号取出来存入自定义的zhanhao变量
mima = mEDit_password.getText().toString(); //将密码取出来存入自定义的mima变量
if (zhanghao.length()<3||zhanghao.length()>7){ //if判断输入账号的长度是不是在3-7位数之间,如果不是则弹窗提示
Toast.makeText(MainActivity.this, "账号长度应为3-7位数之间", Toast.LENGTH_SHORT).show();
}else if (mima.length()<6||mima.length()>6){ //if判断输入账号的长度是不是6位数,如果不是则弹窗提示
Toast.makeText(MainActivity.this,"请输入6位数的密码",Toast.LENGTH_SHORT).show();
}
if (zhanghao.equals("abcdef")&&mima.equals("123456")){ //如果输入的账号密码是“abcdef” “123456” 则实行页面跳转
Intent intent = new Intent(MainActivity.this,dengluMainActivity.class);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"账号或密码输入错误",Toast.LENGTH_SHORT).show();
}
}
});
}
}
嗯!就是这样了,可能有些我没注意讲到,但是大概就是这样了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)