需求说明
项目共有3个页面,第一个主页面,主页面内包含两个按钮button,当点击第一个bt1时,跳转到中间的页面,简称CHANGE页面,当点击bt2时,跳转到第三个页面,简称CAMERA。
全部内容分四部分完成,3个页面为3部分,最后一部分为详细知识点总结。
Adroid Studio 开发说明
- 新建项目
一般为新建Empty空项目 - 项目架构
- 每一个xml文件都对应一个类,启用某个xml页面的时候直接启用xml绑定的对应类,并将这个类在manifests中注册,也就是在Activity标签中添加。
- 主要需要写的就是layout中的xml页面,java包中写xml对应的类,然后在manifests包下的xml文件中进行绑定,一个基础的项目就是如此。
页面1
page_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginTop="256dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="27dp"
android:text="change"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginTop="27dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="352dp"
android:text="camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
xml对应的java类
PageMain.java
package com.example.firstproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class PageMain extends Activity {
/*按钮对象*/
private Button bt1;
private Button bt2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_main); //将指定的xml资源加载到对应的Activity中 page_main为xml文件名
/*
* 通过点击Button对象实现页面之间的跳转
* 1、通过startActivity的方式来实现
* 1>舒适化Intent
* */
bt1 = (Button) findViewById(R.id.button); //绑定bt1中的id 注:"@+id/"此部分不添加
bt2 = (Button) findViewById(R.id.button2);
/*给bt1添加点击监听事件*/
bt1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
* 第一个参数:上下文对象this
* 第二个参数:目标文件*/
Intent intent = new Intent(PageMain.this, MainActivity.class);
/*注:PageMain.this表示当前页面类,MainActivity.class表示要跳转的页面类*/
startActivity(intent);
}
}
);
/*给bt2添加点击监听事件*/
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(PageMain.this, CameraActivity.class);
startActivity(intent);
}
});
/*通过startActivity实现的跳转,此时的页面不需要返回*/
}
}
相关技术
- xml
xml的书写可以直接通过拖拽的方式进行,改变 android:text=" "属性 - Java类
- Activity
- Intent
Intent 是一个消息传递对象,使用它可以向其他Android组件请求操作。Intent的基本用途主要包括:启动 Activity、启动服务、传递广播。 - 监听事件
首先声明xml中的button对象,通过xml中的button _id找到这个这个button,然后复制给这个新声明的button对象。
- 配置AndroidManifests.xml
<!--默认主页面PageMain类-->
<activity
android:name=".PageMain"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第一部分完。