首页 > 其他分享 >创建新的 App 页面

创建新的 App 页面

时间:2024-10-08 22:43:41浏览次数:1  
标签:xml 创建 App import android onCreate 页面

完整的页面创建过程包括三个步骤:

  1. 在 layout 目录下创建 XML 文件

  2. 创建与 XML 文件对应的 Java 代码

  3. 在 AndroidManifest.xml 中注册页面配置

实现两个 Activity 相互跳转的代码:

MainActivity:

package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("cheng", "onCreate: ");
        TextView tv=findViewById(R.id.tv);
        tv.setText("你好, 世界");
        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });
    }
}

MainActivity2:

package com.example.myapplication1;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转" />


</LinearLayout>

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text2"/>

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication1">
        <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=".MainActivity2" />
    </application>

</manifest>

快速生成页面源码:

依次选择右键菜单New→Activity→Empty Activity,弹出图示的页面创建窗口。输入各项信息后,单击窗口右下角的 Finish 按钮,即可完成新页面的创建动作。

标签:xml,创建,App,import,android,onCreate,页面
From: https://www.cnblogs.com/Chengkai730/p/18453149

相关文章

  • uniapp启用蓝牙
    <template> <viewclass="container"> <buttonclass="btn"@click="startBluetoothDevicesDiscovery">搜索蓝牙设备</button> <viewclass="device-list"> <viewv-for="(device,index)i......
  • python小程序个性化服装搭配系统的设计与实现uniapp+flask
    目录项目介绍具体实现截图开发者工具介绍技术路线性能/安全/负载方面开发语言以及框架介绍python-flask核心代码部分展示python-django核心代码部分展示详细视频演示源码获取项目介绍个性化服装搭配方面的任务繁琐,以至于每年都在个性化服装搭配这方面投入较多的精......
  • Linux主机创建新用户并添加root权限
    1.添加用户设置密码useradd-d<homedir>-m-s/bin/bash<username>passwd<username><password>#为该用户设定密码为<password>useradd参数<homedir>:用户家目录,一般为/home/username-s:指定shell到/bin/bash-d:指定其home目录为<homedir>-m:如果指定的home目录......
  • Spring SDK创建和使用完整流程
    SDK创建篇1.spring项目创建2.导入yml文件输入配置信息提示所需要的依赖3.移除pom文件不必要的信息 4.移除启动类5.编写对外提供的客户端类publicclassTestClient{privateStringaccessKey;privateStringsecretkey;publicTestClient(......
  • 【Azure Cloud Service】创建Azure云服务时遇见分配VM资源错误: VM(s) with the follo
    问题描述创建AzureCloudService资源,遇见资源操作完成时的终端预配状态为Failed的信息。创建失败,创建的错误日志截图如下: 详细的错误信息为:{"code":"DeploymentFailed","message":"Atleastoneresourcedeploymentoperationfailed.Pleaselistdeploymentoperati......
  • 大模型应用开发初探 : 基于Coze创建Agent
    大家好,我是Edison。最近学习了一门课程《AIAgent入门实战》,了解了如何在Coze平台上创建AIAgent,发现它对我们个人(C端用户)而言十分有用,分享给你一下。Coze是什么?Coze(扣子)是字节跳动公司开发的新一代AI应用开发平台,使用这个AI应用开发平台,无论你是否有编码基础,都可以快速搭建基......
  • ansible中为什么不都是用shell模块写task,而是创建出一个一个的模块
    ansible的shell模块的功能非常强大,它甚至可以代替ansible的所有模块,比如像unarchive命令,在shell中可以分解为。通过scp命令传送包到远程,再通过tar命令对文件进行解压,再比如user模块可以直接在shell模块中调用useradd命令和usermod命令进行用户的管理,那么为什么还会有其他模......
  • uniapp开发App和h5,项目二期开发中总结的一些小技巧
    1.建议优先nvue开发,先把app端做出来,再去适配h5端。对于不太复杂的页面,一般使用nvue同时兼容app和h5就好了,一个nvue页面的好处就是后续版本迭代更新修改一个页面就行。就是适配的时候有些费事,但用熟练了也还好,总是能找到一些技巧的。2.app端不支持部分H5标签,所以优先使用nvue原......
  • 智能指针的创建
    智能指针是C++中用于自动管理动态分配内存的工具,主要有三种类型:std::unique_ptr、std::shared_ptr和std::weak_ptr。下面是如何创建和使用这些智能指针的详细说明:1.std::unique_ptr用途:std::unique_ptr是一种独占式智能指针,确保指向的对象在同一时间只能被一个指针拥有......
  • 创建索引时需要考虑的关键问题详解
    引言在数据库中,索引是加快数据查询速度的重要工具。通过索引,数据库可以快速定位需要的数据,而无需扫描整个表的数据。尽管索引能极大提高查询效率,但不合理的索引设计也可能导致性能下降,甚至增加不必要的系统开销。尤其在高并发的大规模数据系统中,索引的设计与优化直接关系到......