当前位置: 首页 > news >正文

安卓学习 之 界面切换

今天早晨到现在主要学习的是界面切换。但是效果不太好,也不知道问题出在哪里?不过也需要记录一下,继续学习,后面会了,回来在改吧!问题就是界面有3个,切换到第3个后再回到第一个时就终止了,程序意外崩溃!下图就是整个过程的截图:

今天学习到的切换界面的语句有两条:

第一是创建一个意向:第一个参数:当前所在的Activity,第二个参数:要切换到的Activity

Intent intent = new Intent(test_Activity.this, shuiguo_Activity.class);

第二个是开始一个意向:  参数是:刚刚创建的意向

startActivity(intent);

经过这两句就切换了一个界面。下面来看看代码吧,实际使用这个意向时,发现这两条语句只能使用一次,第二次在用就不好用了,第二次跳转界面我用的是

setContentView(R.layout.shuiguo_layout);我直接把第三个界面给启用了,估计这也是我的程序会崩溃的原因,但是我现在的知识量解决不了这个问题,所以把问题先记下吧,回头能解决了,在回来补充解决方案,下面看代码吧:

第一个界面的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:gravity="center_horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是一个美女"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><ImageViewandroid:id="@+id/imageView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/meinv1" /><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="下一个界面"android:layout_margin="30dp"android:onClick="laohu"/></LinearLayout>

第一个界面的Activity文件:

package com.example.myapplication2;import android.content.Intent;
import android.os.Bundle;
import android.view.View;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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);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;});}public void laohu(View V){Intent intent = new Intent(MainActivity.this, test_Activity.class);startActivity(intent);}}

第二个界面的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是一只老虎" /><ImageViewandroid:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="517dp"android:src="@mipmap/laohu" /><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="下一个界面"android:onClick="shuiguo"/>
</LinearLayout>

第二个界面的Activity文件:

package com.example.myapplication2;import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class test_Activity extends AppCompatActivity {@Overridepublic void onCreate(@Nullable Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.test_layout);}public void shuiguo(View V){//Intent intent = new Intent(test_Activity.this, shuiguo_Activity.class);// startActivity(intent);setContentView(R.layout.shuiguo_layout);}}

第三个界面的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是一个水果"android:layout_gravity="center_horizontal"/><ImageViewandroid:id="@+id/imageView3"android:layout_width="match_parent"android:layout_height="569dp"android:src= "@mipmap/shuiguo" /><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="下一个界面"android:onClick="zhujiemian"/>
</LinearLayout>

第三个界面的Activity文件:

package com.example.myapplication2;import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class shuiguo_Activity extends AppCompatActivity {@Overridepublic void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {super.onCreate(savedInstanceState, persistentState);setContentView(R.layout.shuiguo_layout);}public void zhujiemian(View V){Intent intent = new Intent(shuiguo_Activity.this, MainActivity.class);startActivity(intent);}
}


文章转载自:

http://6bcQ335E.mggwr.cn
http://SD8ivfwV.mggwr.cn
http://rLRaZNMj.mggwr.cn
http://AMks082a.mggwr.cn
http://cEfDVKmE.mggwr.cn
http://b6NJag0t.mggwr.cn
http://HrXFa250.mggwr.cn
http://6aDudie6.mggwr.cn
http://fLyKkEhi.mggwr.cn
http://REVZnJXj.mggwr.cn
http://Qs6vIYH2.mggwr.cn
http://9XNU9zgV.mggwr.cn
http://4To6cAO3.mggwr.cn
http://JCnuMHQa.mggwr.cn
http://EHKq5VW9.mggwr.cn
http://pT9KPpZx.mggwr.cn
http://wfDSG3sy.mggwr.cn
http://pbNG9fuY.mggwr.cn
http://1ZtyUnZw.mggwr.cn
http://G7MlHV8R.mggwr.cn
http://syAUBScB.mggwr.cn
http://VVlmSUqf.mggwr.cn
http://6T2PhsQy.mggwr.cn
http://UXX4Zl2X.mggwr.cn
http://542rPFom.mggwr.cn
http://LZVqTmLU.mggwr.cn
http://NPt5SjWP.mggwr.cn
http://zmYzbRBT.mggwr.cn
http://NZ3gRt8m.mggwr.cn
http://3ANLTtQ6.mggwr.cn
http://www.dtcms.com/a/381448.html

相关文章:

  • 从 IDE 到 CLI:AI 编程代理工具全景与落地指南(附对比矩阵与脚本化示例)
  • 王道数据结构 学习笔记
  • 畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
  • 在springboot中使用mock做controller层单元测试,请求示例包括GET(带参数)、POST(带请求头)、下载文件、上传文件等
  • Kafka 线上问题排查完整手册
  • 数据结构中的排序秘籍:从基础到进阶的全面解析
  • NFS 服务器 使用
  • Zookeeper:分布式协调服务
  • 在 R 语言里,`$` 只有一个作用 按名字提取“列表型”对象里的单个元素 对象 $ 名字
  • 【pure-admin】项目登录模块分析
  • 关于Redis不同序列化压缩性能的对比
  • window显示驱动开发—VidPN 对象和接口
  • 系统架构设计师——【2024年上半年案例题】真题模拟与解析(二)
  • 突破性能瓶颈:基于腾讯云EdgeOne的AI图片生成器全球加速实践
  • JavaScript事件机制与性能优化:防抖 / 节流 / 事件委托 / Passive Event Listeners 全解析
  • 文章目录集合
  • 海外短剧系统开发:技术架构与性能优化实践
  • Windsurf 插件正式登陆 JetBrains IDE:让 AI 直接在你的 IDE 里“打工”
  • 西门子 S7-200 SMART PLC 核心指令详解:从移位、上升沿和比较指令到流水灯控制程序实战
  • 【重要通知】ChatGPT Plus将于9月16日调整全球充值定价,低价区将被弃用,开发者如何应对?
  • 跨省跨国监控难题破解:多层级运维的“中国解法”
  • Spring Boot 与 Elasticsearch 集成踩坑指南:索引映射、批量写入与查询性能
  • 基础算法---【高精度算法】
  • React 18的createRoot与render全面对比
  • 在 React 中如何优化状态的使用?
  • 什么是半导体制造中的PVD涂层?
  • 半导体制造的光刻工艺该如何选择合适的光刻胶?
  • 用图论来解决问题
  • 机器视觉在半导体制造中有哪些检测应用
  • 从废料到碳减排:猎板 PCB 埋容埋阻的绿色制造革命,如何实现环保与性能双赢