GDPU Andriod移动应用 Activity

news2024/9/22 4:45:21

移动应用开发入门级必看,用活动打造属于你的页面。

重要的更新公告

!!!GDPU的小伙伴,感谢大家的支持,希望到此一游的帅哥美女能有所帮助。本学期的前端框架及移动应用,采用专栏订阅量达到50才开始周更了哦( •̀ .̫ •́ )✧ 

 

编译器界面

移动端小白可以看一下,大致了解一下andriod studio的界面布局。在新建工程前,记得要下载好这个编译器还有JDK与SDK,此外,多数用的是虚拟设备,所以还要在device manager里面下载一个虚拟设备便于调试运行。

然后先学一下新建工程,这个敲过代码的都很熟悉了,点击到new project,入门先选empty的布局。

接着输入你的文件名跟路径,还有可选是用java还是kotlin开发的。新建完后,刚入门,看着都晕了,怎么敲,先了解大致要学的文件。

这里选中project,来到工程目录。然后会有很多个文件,版本不同可能有些许差别。

 

在那么多文件中找哪个,作为刚入门的,对准app展开就行了,其它的可以先当配置文件。然后主要的就是里面的main文件。

其中,java文件夹为后端代码放的文件夹,res文件夹放多种资源,包括页面设计代码的编写可放在layout文件夹。然后这里的layout前端页面是支持可视化编程的,所以即使还不太会敲也不要紧哦。可以点击里面默认的xml文件,找到编译器的一些选项帮助开发。

找到这几个选项,版本不同不一定在这个位置。点design进行可视化界面编辑,即在palatte面板找到相应的一些ui组件拖进界面即可,在attributes中设置布局的属性,然后component tree是用来显示页面用的ui控件。点击code即切换到代码模式,andriod中用xml编写,大家可以结合使用设计界面。

然后调好虚拟设备点击运行,即可在设备上显示应用。

在这里写一个运行不了的日志,控制台显示RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data,Failed to load resources table,是sdk与虚拟设备不符的问题。

找到app下的build.gradle,点击查看sdk版本。

 这里的sdk版本要一致,然后调小一点就可以了。

活动的创建

1.启动与销毁Activity

创建一个名为FristActivity的活动

在Manifest文件中注册该活动

重写onCreate方法,并在改方法中加载布局first_layout。

创建布局first_layout.xml,在该布局中添加一个Button控件

在onCreate方法中获得布局中Button的引用

单击Button退出当前Activity

重写onDestroy方法,并在该方法中打印该Activity退出的信息:Activity名称+“退出了,bye!”

 

先新建一个空白活动。

 

看准你设置好的activity name与layout name,代码会自动生成关联的,如果想修改对应的布局文件修改activity代码中对应文件即可。launcher是主页面才需设置的,实验中这个是主页所以勾上,后面创建的不用。

这里activity的代码主要用了java,kotlin也在注释中可参考。注意,FristActivity代码与first_layout是主页代码,包含了后面几题,不再重复打印,里面也有详细注释可参考。

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;


public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = findViewById(R.id.button1);

        Button Button1 = findViewById(R.id.Button1);
        Button Button2 = findViewById(R.id.Button2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 退出当前活动
            }
        });
        //kotlin
//    val button1: Button = findViewById(R.id.button1)
//        button1.setOnClickListener {
//            finish() // 退出当前活动
//        }
        Button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显式 Intent 启动 SecondActivity1
                Intent intent = new Intent(FirstActivity.this, SecondActivity1.class);
                intent.putExtra("message", "Hello SecondActivity");//活动传递数据
                startActivity(intent);
            }
        });

        Button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 隐式 Intent 启动 SecondActivity2
                Intent intent = new Intent("com.example.SECOND_ACTIVITY");
                intent.putExtra("message", "Hello SecondActivity");
                startActivity(intent);
            }
        });

        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
                startActivity(intent); // 启动 ThirdActivity
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("FirstActivity", "FirstActivity退出了,bye!");
    }
    //kotlin
//override fun onDestroy() {
//    super.onDestroy()
//    Log.d("FirstActivity", "FirstActivity退出了,bye!")
//}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

//活动的创建
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1 "/>

//传递数据
    <Button
        android:id="@+id/Button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SecondActivity1" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SecondActivity2" />

//保存恢复
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Activity3" />

</LinearLayout>

最好分开写,这题点击button1。

这题打开控制台的logcat,可查看输出退出的信息。

活动传递数据

创建SecondActivity1和SecondActivity2, SecondActivity1加载布局second_layout1,SecondActivity2加载布局second_layout2。在上题的FirstActivity的布局中添加两个按钮(Button1, Button2),点击Button1时使用显式Intent启动SecondActivity1,点击Button2时使用隐式Intent启动SecondActivity2;此外从FirstActivity中传递一个字符串”Hello SecondActivity”到SecondActivity中,并在SecondActivity中用TextView控件显示。(实验报告中只需写出onCreate方法,实现该功能的代码写在onCreate方法中)

使用显式Intent启动Activity

使用隐式Intent启动Activity

创建活动跟上题类似,然后就是intent的编写,FristActivity加上intent并调用putExtra方法传递数据,然后在SecondActivity进行接收数据。intent的显示启动与隐式启动的区别为,显式 Intent 指定要启动的组件的名称, 而隐式 Intent 指定要执行的操作和数据类型。题中还要在对应的layout代码写一个TextView控件,不会写可以在design界面拉一个TextView控件到界面上。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity1 extends AppCompatActivity {

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

        String message = getIntent().getStringExtra("message");
        TextView textView = findViewById(R.id.textView1);
        textView.setText(message);
        //    val message = intent.getStringExtra("message")
        //        val textView: TextView = findViewById(R.id.textView)
        //        textView.text = message
    }
}
<?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:padding="16dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
</LinearLayout>

 

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity2 extends AppCompatActivity {

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

        String message = getIntent().getStringExtra("message");
        TextView textView = findViewById(R.id.textView2);
        textView.setText(message);
        //    val message = intent.getStringExtra("message")
        //        val textView: TextView = findViewById(R.id.textView)
        //        textView.text = message
    }
}
<?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:padding="16dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
</LinearLayout>

 此外,不要忘了在AndroidManifest进行活动的注册。

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

    <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.T2">
        <activity
            android:name=".ThirdActivity"
            android:exported="false" />
        <activity android:name=".SecondActivity1" />
        <activity
            android:name=".SecondActivity2"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.SECOND_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".FirstActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

点击两个按钮,可跳转到相应的页面,查看Hello SecondActivity的呈现。

 

保存恢复活动状态

假设ThirdActivity中有一个EditText输入框,请按照以上说的方法实现保存和恢复Activity中EditText中的文字。

同样,创建一个活动,然后在FirstActivity写intent跳过去,写好对应的xml与活动的注册。然后,编写保存恢复活动状态在ThirdActivity。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class ThirdActivity extends AppCompatActivity {
    private static final String STATE_TEXT = "saved_text";
    private EditText mEditText;

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

        mEditText = findViewById(R.id.editText);

        // 检查是否为重新创建的实例
        if (savedInstanceState != null) {
            // 恢复 EditText 中的文本
            String savedText = savedInstanceState.getString(STATE_TEXT);
            mEditText.setText(savedText);
            Log.d("ThirdActivity", "Restoring text1: " + savedText);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存 EditText 中的文本
        String textToSave = mEditText.getText().toString();
        outState.putString(STATE_TEXT, textToSave);
        Log.d("ThirdActivity", "Saving text: " + textToSave);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // 恢复文本
        String savedText = savedInstanceState.getString(STATE_TEXT);
        mEditText.setText(savedText);
        Log.d("ThirdActivity", "Restoring text2: " + savedText);
    }
}
<?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:padding="16dp">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入文本" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginTop="16dp"/>
</LinearLayout>

当我们的activity开始Stop,系统会调用 onSaveInstanceState() ,Activity可以用键值对的集合来保存状态信息。当Activity从Destory中重建,我们可以从系统传递的Activity的Bundle中恢复保存的状态。 onCreate() 与 onRestoreInstanceState() 回调方法都接收到了同样的Bundle,里面包含了同样的实例状态信息。  

 

 点击旋转按钮,logcat切换一下,然后看打印出的文本。

实验心得

写了个很长的活动。 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2154129.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

医院伤员小程序点餐———未来之窗行业应用跨平台架构

一、读取服务器医院信息 var 未来之窗人工智-商家信息-医院职工 {//2024-09-22 cyber_getMerchant_CardUser_V20240922: function(appikkey,merchant_id,store_id,ecogen_sponsor_appid,openid,frommsg,wlzc_callback) {//2023-7-6 里程碑var wlzcapi"加入url";wx.re…

深度学习自编码器 - 去噪自编码器篇

序言 在深度学习的广阔天地中&#xff0c;自编码器作为一种强大的无监督学习工具&#xff0c;通过重构输入数据的方式&#xff0c;不仅实现了数据的有效压缩&#xff0c;还探索了数据的内在表示。而去噪自编码器&#xff08; Denoising Autoencoder, DAE \text{Denoising Auto…

ES6 -- 2015

学习视频 1. let和const 1.1 let 变量必须先声明再使用同一变量不能重复声明变量有块级作用域 1.2 const 声明常量&#xff0c;常量不能改变常量必须有初始值&#xff0c;不能先声明再赋值 2. 解构 1 数组解构 保持左右的结构一样&#xff0c;安装顺序一一对应 完全解构…

CVE-2024-46101

前言 自己挖的第一个CVE~ 喜提critical 这里简单说一下。 漏洞简介 GDidees CMS < 3.9.1 的版本&#xff0c;存在一个任意文件上传漏洞。允许登录后的攻击者上传webshell获得网站的权限。 影响版本&#xff1a; GDidees CMS < 3.9.1 &#xff08;其它的我没测。。&am…

日志系统扩展二:日志服务器的实现

日志系统扩展二&#xff1a;日志服务器的实现 一、设计1.为何要这么扩展&#xff1f;2.应用层协议的选择1.HTTP&#xff1f;2.自定义应用层协议 二、自定义应用层协议的实现1.声明日志器1.服务器和客户端这里日志器的关联2.枚举类型的定义3.sinks数组的定义 2.打印日志1.logMes…

MySQL record 06 part

事务、存储过程 事务&#xff1a; MySQL的同步&#xff0c;同步是指 together done&#xff0c;要么一起前进&#xff0c;要么一起后退的意思。 注意&#xff0c;回滚 rollback 对已经提交 commit 的数据是无效的&#xff0c;也就是说&#xff0c;只能对没有被提交 commit …

CSS 布局三大样式简单学习

目录 1. css 浮动 1.1 效果1 1.2 效果2 1.3 效果3 1.4 效果4 2. css 定位 2.1 absolute 2.2 relative 2.3 fixed 3. css 盒子模型 3.1 效果1 3.2 效果2 3.3 效果3 3.4 效果4 1. css 浮动 1.1 效果1 1.2 效果2 1.3 效果3 1.4 效果4 2. css 定位 2.1 absolute 2.2 …

thinkphp 做分布式服务+读写分离+分库分表(分区)(后续接着写)

thinkphp 做分布式服务读写分离分库分表&#xff08;分区&#xff09; 引言 thinkphp* 大道至简一、分库分表分表php 分库分表hash算法0、分表的方法&#xff08;thinkphp&#xff09;1、ThinkPHP6 业务分表之一&#xff1a;UID 发号器2、ThinkPHP6 业务分表之二&#xff1a;用…

希尔排序(C语言实现)

目录 1.希尔排序( 缩小增量排序 ) 2.动图 ​编辑 3.代码实现 预排序实现 子序列排列实现 单趟排序实现 对整组数进行子排序 希尔排序代码 代码测试 时间复杂度分析 希尔排序的特性总结&#xff1a; 1.希尔排序( 缩小增量排序 ) 基本思想&#xff1a; 1.先选定一个…

QTCreator 调试:unknown debugger type “No engine“

QTCreator 调试&#xff1a;unknown debugger type "No engine" - kaizenly - 博客园 (cnblogs.com) 一开始Debuggers---Auto-detected这里第一row第一个项是标红的&#xff0c;然后没改东西&#xff0c;点完应用Apply以后&#xff0c;就可以调试了...&#xff08;不…

在python爬虫中xpath方式提取lxml.etree._ElementUnicodeResult转化为字符串str类型

简单提取网页中的数据时发现的 当通过xpath方式提取出需要的数据的text文本后想要转为字符串&#xff0c;但出现lxml.etree._ElementUnicodeResult的数据类型不能序列化&#xff0c;在网上查找到很多说是编码问题Unicode编码然后解码什么的&#xff1b;有些是(导入的xml库而不…

深度学习之概率论预备知识点(3)

在深度学习中&#xff0c;概率论和数理统计是理解许多算法背后的理论基础。这些知识在处理不确定性、估计模型参数、理解数据分布等方面非常关键 1、概率 一种用来描述随机事件发生的可能性的数字度量&#xff0c;表示某一事件发生的可能性。 概率并不客观存在&#xff0c;是…

Android Choreographer 监控应用 FPS

Choreographer 是 Android 提供的一个强大的工具类&#xff0c;用于协调动画、绘制和视图更新的时间。它的主要作用是协调应用的绘制过程&#xff0c;以确保流畅的用户体验。Choreographer 也可以帮助我们获取帧时间信息&#xff0c;从而为性能监测和优化提供重要的数据支持。 …

IDEA中Quarkus框架(3.13版本)开发、调试、部署、打包等

code-with-quarkus code-with-quarkus 是使用官网生成的demo项目 这个项目使用Quarkus&#xff08;使用3.13.0版本&#xff0c;该版本支持JDK21&#xff09;&#xff0c;超音速亚原子Java框架。官网地址: https://quarkus.io/. 环境要求 OS: Windows 10.0 jdk 11 maven 3.9…

淘宝扭蛋机小程序,扭蛋机文化下的新体验

在数字化时代中&#xff0c;扭蛋机逐渐从传统的线下机器转移到了线上互联网中&#xff0c;市场得到了创新发展。扭蛋机小程序具有便捷、多样化、个性化的特点&#xff0c;迎合了当下消费者的线上消费习惯&#xff0c;又能够让扭蛋机玩家体验到新鲜有趣的扭蛋。 扭蛋机是一种热…

python简单的小项目-关于央行储蓄占比情况的数据可视化

该数据来源于锐思数据库&#xff0c;如果数据有偏差&#xff0c;可能是本人搜索的问题&#xff0c;希望大家谅解。 数据大纲&#xff1a; 其中我们制作折现统计图需要用到的是截止日期&#xff0c;表达数据最后获取的日期&#xff0c;而更新时间则是数据时效性的表示&#xff…

django项目添加测试数据的三种方式

文章目录 自定义终端命令Faker添加模拟数据基于终端脚本来完成数据的添加编写python脚本编写shell脚本执行脚本需要权限使用shell命令来完成测试数据的添加 添加测试数据在工作中一共有三种方式&#xff1a; 可以根据django的manage.py指令进行[自定义终端命令]可以采用第三方…

pthread_cond_signal 和pthread_cond_wait

0、pthread_join()函数作用&#xff1a; pthread_join() 函数会一直阻塞调用它的线程&#xff0c;直至目标线程执行结束&#xff08;接收到目标线程的返回值&#xff09;&#xff0c;阻塞状态才会解除。如果 pthread_join() 函数成功等到了目标线程执行结束&#xff08;成功获取…

【C++】list详解及模拟实现

目录 1. list介绍 2. list使用 2.1 修改相关 2.2 遍历 2.3 构造 2.4 迭代器 2.5 容量相关 2.6 元素访问 2.7 操作相关 3. 模拟实现 3.1 节点类 3.1.1 初始结构 3.1.2 节点的构造函数 3.2 迭代器类 3.2.1 初始结构 3.2.2 迭代器 3.2.3 迭代器-- 3.2.4 解引…

1.随机事件与概率

第一章 随机时间与概率 1. 随机事件及其运算 1.1 随机现象 ​ 确定性现象&#xff1a;只有一个结果的现象 ​ 确定性现象&#xff1a;结果不止一个&#xff0c;且哪一个结果出现&#xff0c;人们事先并不知道 1.2 样本空间 ​ 样本空间&#xff1a;随机现象的一切可能基本…