首页 > 下载 > 下载详文:Android打开新的Activity窗口

Android打开新的Activity窗口

发布时间:2024年12月29日 17时43分15秒   属性:移动/手机应用开发 > Android    访问次数:7404
字体: 初始 添加收藏 分享给好友

Android应用从一个界面打开新的Activity, 在桌面应用和web和页面上,都可以从窗体或页面打开另一个新的窗体,Android打开新的是Activity,本文示例通过按钮onClick事件打开新的Activity窗口,在Android Studio中新建项目默认生成“MainActivity”,然后再新建一个“FullscreenActivity”,用于打开新的Activity窗口切换。本文属于Android开发的基础篇,需要对Android Studio有基本入门(如:新建项目、文件等), 并且对Java熟练才能顺利上手阅读。

先在Android Studio中新建项目,项目名称命名为“ActivityOpen”,可以根据自己随意命名,创建项目后,自动生成了“MainActivity”和“activity_main.xml”,先将“activity_main.xml”中默认的“TextView”删除,加入“Button”按钮,上下居中对齐,将其id命名为“btnOpen”,按钮用来点击打开新的“Activity”。代码如下:

activity_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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

如下图:

接着在“Project”项目目录中右键“New”新建“Activity”文件,在“Activity”中选择“Fullscreen Views Activity”,为默认全屏模式,用于MainActivity中的按钮点击打开用。代码如下:

activity_fullscreen.xml 代码  复制
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background=
"?attr/fullscreenBackgroundColor"
android:theme=
"@style/ThemeOverlay.ActivityOpen.FullscreenContainer"
tools:context=
".FullscreenActivity">

<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->

<TextView
android:id=
"@+id/fullscreen_content"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:gravity=
"center"
android:keepScreenOn=
"true"
android:text=
"@string/dummy_content"
android:textColor=
"?attr/fullscreenTextColor"
android:textSize=
"50sp"
android:textStyle=
"bold" />

<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->

<FrameLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:fitsSystemWindows=
"true">

<LinearLayout
android:id=
"@+id/fullscreen_content_controls"
style=
"@style/Widget.Theme.ActivityOpen.ButtonBar.Fullscreen"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|center_horizontal"
android:orientation=
"horizontal"
tools:ignore=
"UselessParent">

<Button
android:id=
"@+id/dummy_button"
style=
"?android:attr/buttonBarButtonStyle"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"@string/dummy_button" />

</LinearLayout>
</FrameLayout>

</FrameLayout>

在“activity_fullscreen.xml”中“TextView”文字值绑定的"@string/dummy_content",activity的背景颜色绑定的"?attr/fullscreenBackgroundColor",在strings.xml文件中设置,如下:

string.xml 代码  复制
<resources>
<string name="app_name">ActivityOpen</string>
<string name="title_activity_fullscreen">FullscreenActivity</string>
<string name="dummy_button">Dummy Button</string>
<string name="dummy_content"> | 遺昕传媒 | weisim3.com</string>
</resources>

FullscreenActivity的全屏模式,知识点如何进入全屏隐藏状态栏,这里可以参考下“run() ”方法,详细代码如下:

Java 代码  复制
 private View mContentView;
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
// Delayed removal of status and navigation bar
if (Build.VERSION.SDK_INT >= 30) {
mContentView.getWindowInsetsController().hide(
WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
} else {
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}
};
private View mControlsView;
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};

Activity准备工作做好之后, 回到“MainActivity”中将btnOpen按钮实例化,加入按钮事件触发打开新的Activity,这里按钮点击打开的是“FullscreenActivity”,代码如下:

Java 代码  复制
package com.example.activityopen;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(
this);
setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.btnOpen);

/* Android打开新的Activity窗口(12.24.2024)
* Copyright (C) 遗昕传媒 | weisim3.com */

button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=
new Intent(MainActivity.this,FullscreenActivity.class);
startActivity(intent);
finish();//退出结束主程
}
});


ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}

最终效果如下:

Android打开新的Activity窗口 (1)
本下载连接不支持第三下载工具打开,请直接点击下载即可
文章版权归属weisim3.com所有,未经书面版权许可同意,不得私自转载(或做修改转载),源文件示例仅供学习使用,更不要出于商业用途或印刷出版发行!否则将追究其相关法律责任,版权联系:729260499。
遺昕 | Weisim3.com 下载许可条款 ( 您本次需要付费下载 ) .



付费源文件: Android打开新的Activity窗口
支付金额: ¥3.00        授权期限: 3
3B0eOxXTuIRZTr8qXJYcUz6UncPJgv -- 20260212110948