MainActivity文件
package com.example.test; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { int a=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取按钮 Button button=findViewById(R.id.xianshi); //获取对象,这里对象是密码 EditText text=findViewById(R.id.mima); //为按钮注册点击事件监听器 //new View.OnClickListener()匿名内部类 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (a%2==0){ //如果选中,则显示密码 text.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); a++; }else{ //否则,隐藏密码 text.setTransformationMethod(PasswordTransformationMethod.getInstance()); a++; } //点击显示密码后,光标会回到最前面,而这个方法使光标回到最后面 text.setSelection(text.length()); } }); } public void denglu(View view){ //判断姓名密码是否为空 //获取文本框内容 EditText nameEdit=findViewById(R.id.name); EditText mimaEdit=findViewById(R.id.mima); String name=nameEdit.getText().toString(); String mima=mimaEdit.getText().toString(); //判断账号与密码是否为空 if(name.equals("") || mima.equals("")){ //无焦点提示信息,例如,账号或密码为空 //参数1,环境上下文,例如,这个对象要在当前页面展示出来,所以这个环境为MainActivity,所以填this/ //参数2,提示性文本 //参数3,提示时间 Toast.makeText(this, "姓名或密码不能为空", Toast.LENGTH_SHORT).show(); } else{ //指定文本框中的与自己设定的密码比较,相同则登录成功,不同则表示失败 if(name.equals("admin") || mima.equals("123456")){ //这个是跳转页面,跳转到你所建立的SecondActivity的页面中 Intent intent = new Intent(MainActivity.this, SecondActivity.class); //启动Intent startActivity(intent); }else{ Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } } }
activity_second页面,创作的跳转页面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".SecondActivity" android:background="@drawable/d97d6aed0d86908b" android:orientation="vertical"> </LinearLayout> activity_main页面,登录页面设计的地方
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/d97d6aed0d86908b" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:layout_marginTop="80dp" android:text="Sign up" android:textColor="#00BCD4" android:textSize="80sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Imooc Imooc Imooc Imooc Imooc " android:textColor="#00BCD4" android:textSize="34sp" /> /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="68dp" android:layout_gravity="center" android:layout_marginLeft="60dp" android:layout_marginTop="80dp" android:hint="Name and Surname" android:inputType="textVisiblePassword" android:maxLength="10" android:textColor="#00ff00" android:textColorHint="#cccccc" /> <ImageView android:layout_width="35dp" android:layout_height="35dp" android:layout_marginLeft="34dp" android:layout_marginTop="-50dp" android:src="@mipmap/denglubiao" /> <ImageView android:layout_width="35dp" android:layout_height="35dp" android:layout_marginLeft="34dp" android:layout_marginTop="70dp" android:src="@mipmap/password" /> <EditText android:id="@+id/mima" android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="60dp" android:layout_marginTop="-50dp" android:hint="Enter your password" android:inputType="textPassword" android:textColor="#00ff00" android:textColorHint="#cccccc" /> <Button android:id="@+id/xianshi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示密码" android:textColor="#00ffff" android:layout_marginLeft="60dp" android:layout_marginTop="60dp" /> <Button android:id="@+id/qumima" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="280dp" android:layout_marginTop="-45dp" android:text="忘记密码?" android:textColor="#00ffff" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="登录" android:textColor="#00ffff" android:onClick="denglu"/> </LinearLayout>
登录页面
跳转到的页面