1.界面布局
在默认创建的activity_main.xml代码中将界面布局搭建好,可以选择在design视窗中直接拖动布局,也可以在text代码界面撸代码布局,具体如下:
首先将布局改为Linearlayout线性布局或者Relatvelayout相对布局或者其他布局模式,则对应的代码做相应的调整,我这里用的Linearlayout线性布局。
design界面如图:
将activity_main.xml界面的id属性设置规范,后面会用,代码如下:
<?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"
android:gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="用户名:" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="请输入用户名"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="密码 :" />
<EditText
android:id="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<Button
android:onClick="login"
android:id="@+id/btn_login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:onClick="finlish"
android:id="@+id/btn_finlish"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|right|end"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2.创建跳转后的界面
3.在MainActivity.java界面中编写跳转及登录代码
package com.example.myapplication777;
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 myname;
private EditText mypass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局文件
setContentView(R.layout.activity_main);
//查找控件
myname=findViewById(R.id.name);
mypass=findViewById(R.id.pass);
}
// 登录点击事件
public void login (View v)
{
//点击输入用户名和密码,验证正确后才可以跳转
String sname=myname.getText().toString().trim();
String spass= mypass.getText().toString().trim();
if (sname.equals("admin")&&spass.equals("123456"))
{
//用户名和密码正确后跳转到Main2
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent= new Intent(MainActivity.this,Main2Activity999.class);
startActivity(intent);
finish();
}
else
{
Toast.makeText(MainActivity.this,"输入错误 ",Toast.LENGTH_SHORT).show();
}
}
public void finlish (View w)
{
/*Toast.makeText(MainActivity.this,"tichu",Toast.LENGTH_LONG).show();*/
System.exit(0);
}
}
正常情况应该是输入用户名:admin 密码:1234567 显示登录成功并跳转界面