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、测试结果

启动界面

Android实现页面跳转的全过程记录

输入消息“123”并点击按钮发送,接收界面如下

Android实现页面跳转的全过程记录 

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)

Android实现页面跳转的全过程记录

发送附加消息到111

Android实现页面跳转的全过程记录

发送成功

Android实现页面跳转的全过程记录

来源:https://blog.csdn.net/qq_45974648/article/details/120790383

标签:android,页面,跳转
0
投稿

猜你喜欢

  • SpringBoot实现过滤器和拦截器的方法

    2022-10-21 23:29:34
  • java实现KFC点餐系统

    2021-09-06 11:36:34
  • Java+opencv3.2.0实现人脸检测功能

    2022-11-27 10:36:42
  • Java 定时器(Timer,TimerTask)详解及实例代码

    2022-08-17 20:03:11
  • C#探秘系列(一)——ToDictionary,ToLookup

    2023-04-19 09:16:15
  • 浅谈java中OO的概念和设计原则(必看)

    2023-11-24 13:09:56
  • Android的ImageButton当显示Drawable图片时就不显示文字

    2023-11-25 17:40:00
  • MyBatis-Plus 查询返回实体对象还是map

    2023-11-28 03:20:19
  • Java实现简单文件过滤器功能

    2021-09-13 23:27:58
  • springBoot整合redis使用案例详解

    2022-07-02 10:51:33
  • 浅谈java线程状态与线程安全解析

    2023-05-11 19:20:25
  • 详解在Spring-Boot中实现通用Auth认证的几种方式

    2023-10-08 00:48:28
  • java 创建自定义数组

    2022-09-02 11:18:45
  • C#二进制读写BinaryReader、BinaryWriter、BinaryFormatter

    2022-03-07 23:01:28
  • 使用IDEA开发配置Java Web的初始化过程

    2022-09-25 16:33:38
  • springboot实现定时任务的四种方式小结

    2021-10-20 20:38:06
  • Java精品项目瑞吉外卖之员工信息管理篇

    2023-07-29 07:43:36
  • C#实现绘制鼠标的示例代码

    2023-06-11 04:40:54
  • Java高级架构之FastDFS分布式文件集群详解

    2023-07-23 14:57:50
  • android studio 4.0 新建类没有修饰符的方法

    2023-02-06 04:56:52
  • asp之家 软件编程 m.aspxhome.com