将前面写完的登陆界面拿过来进行完善
ability_main.xml代码:
在原代码的基础上全部加上id
<TextField
ohos:id="$+id:username"
/>
<TextField
ohos:id="$+id:password"
/>
<Text
ohos:id="$+id:forgetpassword"
/>
<Button
ohos:id="$+id:login"
/>
<Button
ohos:id="$+id:register"
/>
把前面写的吐司弹框拿过来用一下,放入以下文件夹
MainAbilitySlice.java代码:
package com.example.textfieldapplication.slice;
import com.example.textfieldapplication.ResourceTable;
import com.example.textfieldapplication.toastUtils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
TextField username;
TextField password;
Text forgetPassword;
Button register;
Button login;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到五个组件
username = (TextField) findComponentById(ResourceTable.Id_username);
password = (TextField) findComponentById(ResourceTable.Id_password);
forgetPassword = (Text) findComponentById(ResourceTable.Id_forgetpassword);
register = (Button) findComponentById(ResourceTable.Id_register);
login = (Button) findComponentById(ResourceTable.Id_login);
//2.给三个组件添加单击事件
forgetPassword.setClickedListener(this);
login.setClickedListener(this);
register.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
if(component == forgetPassword){
//跳转到忘记密码页面
ToastUtils.showDialog(this,"点击了忘记密码");
}else if(component == login){
//比较用户输入的用户名密码是否正确
String usernameInput = username.getText();
String passwordInput = password.getText();
//比较,在实际开发中,把用户名密码传递给服务器比较,现在的知识还做不到
//做一个写死的用户名密码 zhangsan 123456
if("zhangsan".equals(usernameInput) && "123456".equals(passwordInput)){
//输入正确,进入APP
ToastUtils.showDialog(this,"登陆成功");
}else{
//有错误,给用户提示:吐司弹框
if("".equals(usernameInput) || "".equals(passwordInput)){
ToastUtils.showDialog(this,"用户名和密码不能为空");
}else{
ToastUtils.showDialog(this,"用户名或密码错误了");
}
}
}else if(component == register){
//跳转到注册页面
ToastUtils.showDialog(this,"点击了注册");
}
}
}
效果:这里只展示输入正确的情况