Android入门小案例,制作登陆页面跳转

这个案例,主要目的是带着大家理解Android开发的基础知识,与UI布局以及点击事件,方便大家能更好的入门。至于如何创建一个Android项目这里就不在过多阐述。笔者这里使用的开发工具是IDEA,语言选的是java。如果还有朋友不会如何搭建项目,想知道详情可以点击链接看这片文章。

https://www.jb51.net/article/196746.htm

进入正题,首先给大家看一下项目结构

在mylinearlayout目录下有两个类HomePage与MainActivity两个java类

res文件夹下的layout放着两个xml文件,分别是HomePage与MainActivity的布局文件。

安卓开发的布局xml,其实就相当于网页的HTML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"  //填充满父级元素
        android:layout_height="match_parent" //填充满父级元素
        android:gravity="center" //居中
        android:orientation="vertical" //纵向排列
>
    <LinearLayout
            android:layout_width="200dp" //设置宽度为200dp
            android:layout_height="100dp"
            android:gravity="center"

    >
        <TextView                              //文字控件
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:textSize="20dp"
                android:text="账号"       //控件文字描述
                android:gravity="center"

        />
        <EditText
                android:layout_height="100dp"
                android:layout_width="100dp"
                android:layout_marginBottom="30dp"
        />
    </LinearLayout>
    <LinearLayout
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:gravity="center"

    >
        <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:textSize="20dp"
                android:text="密码"
                android:gravity="center"

        />
        <EditText                                      //文本框控件
                android:layout_height="100dp"
                android:layout_width="100dp"
                android:layout_marginBottom="30dp"
        />
    </LinearLayout>
    <Button                                           //按钮控件
            android:id="@+id/mybutton"
            android:layout_width="200dp"
            android:layout_height="80dp"
            android:text="登陆"
            android:layout_marginTop="50dp"
    >

    </Button>

</LinearLayout>

大家看是不是很像。最上层的父级元素LinearLayou叫线性布局,就相当于一个Html里的一个<div>标签。其中还有

至于里面的子元素是行级元素还是块级元素,取决于LinearLayout 标签里面的这个属性android:orientation。

当你设置为vertical就是纵向排列,当你设置为horizontal就是水平排列。

 那设置完布局xml后,该怎么用呢?

我需要回到我们的MainActivity类中将这个文件塞入setContentView这个方法里面

 onCreate为可以是一个生命周期方法,在该应用最初创建的时候被自动调用,我们可以将一些初始化的逻辑写在这里面。

接下来我们要给button组件添加一个点击事件,以完成登录跳转的功能

我们在xml布局文件中给button组件定义了一个id,现在可以根据id获取这个组件,并监听按钮是否被按下。

实现一个接口,并重写里面的一个onclick()方法

 在该方法中写跳转逻辑。

 以下是MainActivity完整代码

package com.example.mylinearlayout;

import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private  Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intiView();
    }

    public void intiView(){
        button = findViewById(R.id.mybutton);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(this,"您点击了一个按钮",Toast.LENGTH_LONG).show();
        startActivity(new Intent(this,HomePage.class));
    }
}

现在我们再来设置HomePage的布局

package com.example.mylinearlayout;

import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class HomePage extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage_main);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".HomePage">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

最后最重要的一点,别忘了在AndroidManifest.xml文件中把我们的HomePage给注册进去

       <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".HomePage"></activity>
    </application>

最后效果图就是这样的

点击跳转

 

今天为大家分析的案例就讲到这里,后续还会分享如何设置精美的样式如何发起网络请求等更多内容,喜欢的给个赞,下次再见! 

  • 13
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值