登陆界面的布局代码:
xml的代码
目录
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bj5"
android:layout_gravity="center_horizontal"
>
<ImageView
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_gravity="center"
android:src="@mipmap/touxiang"
android:layout_marginTop="100dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/denglu"/>
<EditText
android:id="@+id/zh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="请输入账户名 "
android:onClick="zhjianting"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="60dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/mima"
android:layout_gravity="center"/>
<EditText
android:id="@+id/mm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30sp"
android:hint="请输入密码"
android:onClick="mmjianting"
android:inputType="numberPassword" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<CheckBox
android:id="@+id/xs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="显示密码"
android:textSize="20sp" />
<Button
android:id="@+id/forget_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/xs"
android:layout_toRightOf="@id/xs"
android:text="忘记密码"
android:layout_marginLeft="100dp"
android:textSize="20sp" />
</RelativeLayout>
<Button
android:id="@+id/bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30dp"
android:layout_marginTop="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
CheckBox是复选框。
MyActivity的代码:
package com.example.myapplication;
/*
Toast是为了有好的提醒用户,而又不影响用户的操作的。
方法中的MainActivity.this表示在MainActivity里显示;
R.string.alert表示引用string下的alert中的资源;
LENGTH_SHORT表示Toast的显示时间一微秒计算,这里调用系统定义好扥时间,也可自己写入确定的时间。
关于.show(); 是用来将定义好的Toast显示在MainActivity里的,
如果不调用.show();方法定义的Toast就没有意义了。
*/
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
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.Toast;
public class MainActivity extends AppCompatActivity {
Intent intent;
String account="admin";
String password="123456";
String mima;
String zhanghu;
EditText m;
EditText z;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jiemian_layout);
//显示密码-----------------------------------------------------------------------------------
CheckBox xianshi=findViewById(R.id.xs);
xianshi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
//如果选中,显示密码
m.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//否则隐藏密码
m.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
//-----------------------------------------------------------------------------------------
//账户
z=findViewById(R.id.zh);
z.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable editable) {
zhanghu=z.getText().toString();
}
});
//-----------------------------------------------------------------------------------------
//密码
m=findViewById(R.id.mm);
m.addTextChangedListener(new TextWatcher() {
@Override
//输入框改变前
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
//输入框改变时
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
//输入框改变后
public void afterTextChanged(Editable editable) {
//获取输入框改变后的信息
mima=m.getText().toString();
}
});
//-----------------------------------------------------------------------------------------
//获取按钮
Button bin=findViewById(R.id.bin);
bin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(zhanghu.equals(account)&&mima.equals(password)){
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
//页面跳转(当前页面.this,要跳转的页面.class)-------------------------------------------
Intent intent=new Intent(MainActivity.this,MianActivity2.class);
//启动Intent
startActivity(intent);
}else{
if(zhanghu.equals(account)){
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
}else if(mima.equals(password)){
Toast.makeText(MainActivity.this, "账户名错误", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "账户名,密码都错误", Toast.LENGTH_SHORT).show();
}
}
Intent intent=new Intent(MainActivity.this,MianActivity2.class);
startActivity(intent);
}
});
//-----------------------------------------------------------------------------------------
}
}
Toast是为了有好的提醒用户,而又不影响用户的操作的。 方法中的MainActivity.this表示在MainActivity里显示; R.string.alert表示引用string下的alert中的资源; LENGTH_SHORT表示Toast的显示时间一微秒计算,这里调用系统定义好扥时间,也可自己写入确定的时间。 关于.show(); 是用来将定义好的Toast显示在MainActivity里的, 如果不调用.show();方法定义的Toast就没有意义了。
z.addTextChangedListener(new TextWatcher() {});输入框改变的监听事件
CheckBox的setOnCheckedChangeListener监听器:setOnCheckedChangeListener
获取输入框的文本信息时用getText().toString()方法
密码的隐藏及显示
if (b) { //如果选中,显示密码 m.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { //否则隐藏密码 m.setTransformationMethod(PasswordTransformationMethod.getInstance()); }
网页的跳转
用Intent实现界面的跳转
Intent intent=new Intent(MainActivity.this,MianActivity2.class);
//(当前页面.this,将要跳转的界面.class)
startActivity(intent);