Android实现页面跳转的全过程记录
作者:Dannin_pqf 发布时间:2023-08-16 21:06:08
1、启动新Activty
1.1、功能分析
App功能
在第一个Activity输入消息
点击第一个Activity的发送按钮
发送消息到第二个Activity
第二个Activity显示收到的消息
App结构(2个Activity+2个Layout) :
打开App时,启动CreateMessageActivty
加载activity_create_message.xml作为布局用户点击按钮启动ReceiveMessageActivty
加载activity _receive_message.xml作为布局
1.2、开发视图布局
activity_create_message.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"
tools:context=".CreateMessageActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint"
android:inputType="textPersonName"
android:textSize="30sp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSendMessage"
android:text="@string/send"
android:textSize="30sp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity _receive_message.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"
app:layout_constraintRight_toRightOf="parent"
tools:context=".ReceiveMessageActivity">
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Activity"
android:textSize="34sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constr
string.xml
<resources>
<string name="app_name">Messager</string>
<string name="send">Send Message</string>
<string name="hint">Enter a message</string>
<string name="choser">Send Message via ...</string>
</resources>
1.3、按钮事件响应
CreateMessageActivty类:发送消息
public class CreateMessageActivity extends AppCompatActivity {
//定义常量,作为消息的key
public static final String MESSAGE_KEY="szst.it.ping.messager";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//获得编辑框引用
EditText editText = findViewById(R.id.input);
//取出编辑框文字
String message = editText.getText().toString();
//Intent是Android中的信使,新建Intent打开,设置收件Activity为ReceiveMessageActivity
Intent intent = new Intent(this,ReceiveMessageActivity.class) ;
//在intent中附加消息
intent.putExtra(MESSAGE_KEY,message);
//向Android发出请求
startActivity(intent);
}
}
ReceiveMessageActivty类:接收消息
public class ReceiveMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_message);
//获得intent的引用
Intent intent = getIntent();
//根据key取出value
String message = intent.getStringExtra(CreateMessageActivity.MESSAGE_KEY);
//获得文本框内容,设置文字
TextView textView = findViewById(R.id.output);
textView.setText(message);
}
}
1.4、测试结果
启动界面
输入消息“123”并点击按钮发送,接收界面如下
2、启动其他App
2.1、功能分析
App功能
在第一个Activity输入消息
点击第一个Activity的发送按钮
发送消息到其他App
其他App显示收到的消息
App结构(1个Activity+1个Layout) :
打开App时,启动CreateMessageActivty
加载activity_create_message.xml作为布局用户点击按钮启动选择启动满足条件的App
2.2、开发视图布局
activity_create_message.xml
同1.2中的activity_create_message.xml
2.3、按钮事件响应
CreateMessageActivty类:发送消息
public class CreateMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//获得编辑框引用
EditText editText = findViewById(R.id.input);
//取出编辑框文字
String message = editText.getText().toString();
//使用new Intent(Intent.ACTION_SEND)替换new Intent(this, ReceiveMessageActivity.class),不知道其它App中的类名
Intent intent = new Intent(Intent.ACTION_SEND);
//设置消息类型为纯文本,系统不会对消息进行处理
intent.setType("text/plain");
//向Intent添加附加信息
intent.putExtra(Intent.EXTRA_TEXT,message);
//自定义选择对话框
String chooserTitle = getString(R.string.choser);
Intent chosenIntent = Intent.createChooser(intent, chooserTitle);
startActivity(chosenIntent) ;
}
}
2.4、测试结果
启动界面同1.4
输入消息“123”并点击按钮发送,选择要发送的app(Messaging)
发送附加消息到111
发送成功
来源:https://blog.csdn.net/qq_45974648/article/details/120790383


猜你喜欢
- 1.配置多个数据源多个数据源是指在同一个系统中,用户数据来自不同的表,在认证时,如果第一张表没有查找到用户,那就去第二张表中査询,依次类推。
- 前言值类型和引用类型,是c#比较基础,也必须掌握的知识点,但是也不是那么轻易就能掌握,今天跟着老胡一起来看看吧。 典型类型首先我们
- 前言前一阵项目中的上传图片改为上传到阿里上,记录一下实现的过程,方便以后查看。参考资料:官方文档配置Android studio添加依赖de
- 昨天直接在机器上配置了Maven环境,今天顺便把Eclipse等IDE环境配置好。安装IDE Plugins的方法有很多。其一:在线安装,通
- sum(参数) 列名作为参数项目中有很多个字段,当字段为空的时候,求该列的平均值并赋值给该字段。如: id
- 在这篇文章中,我将向您展示如何用新的Java 8 forEach语句循环一个List和Map。1、forEach 和 Map1.1、常规循环
- 1、springboot controller 单例Spring中 controller默认是单例的,因为单例所以不是线程安全的。所以需要注
- 1. 简介Redis 是一个开源(BSD许可)的,内存中的key-value存储系统,它可以用作数据库、缓存和消息中间件。2. 对key的操
- 静态库和动态库的区别1、静态库的扩展名一般为".a"或者".lib";动态库的扩展名一般为"
- 本文实例讲述了C#远程获取图片文件流的方法。分享给大家供大家参考,具体如下:protected void Page_Load(object
- public void CutToF(Stream stream)
- 相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,
- 我遇到一个重复性操作,为了能偷懒发现idea的功能还比较实用纵列选择:Alt+鼠标左键大小写转换:Ctrl+Shirt+u使用小技巧:像这样
- 一、Hadoop的安装1. 下载地址:https://archive.apache.org/dist/hadoop/common/我下载的是
- 一般数据库的编码是utf8,utf8是不支持存储表情符的,当存入的微信昵称带有表情符时就会出现乱码情况,有两种解决方法:1.mysql数据库
- 前言最近项目中需要用到字符串加解密,遂研究了一波,发现密码学真的是博大精深,好多算法的设计都相当巧妙,学到了不少东西,在这里做个小小的总结,
- 静态数组Java中最基本的数组大家肯定不会陌生:int[] array = new int[6];for (int i = 0; i <
- 本文实例讲述了C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题。分享给大家供大家参考,具体如下:一、理论
- 前言:2022年Java将有什么新的特性和改进,我相信很多Java开发者都想知道。结合Java语言架构师布莱恩·格茨(
- — 遇到问题今天在IDEA里面运行项目的时候报了一个错,如下图所示:— 找到问题根源其实控制台给出的错误信息提示说的很明显:类加载器加载文件