原文链接:https://blog.csdn.net/weixin_38420342/article/details/84344496

一、切换Activity的5种方式

Intent intent = new Intent();
(1)intent.setClass(this,OtherActivity.class);
(2)intent.setClassName(this,"com.xiazdong.OtherActivity");
(3)intent.setClassName("com.xiazdong","com.xiazdong.OtherActivity");//此种方式用来激活不同应用的Activity,只需要指定第一个参数:包名 为另一个应用即可;
(4)
Component comp = new Component(this,OtherActivity.class);
intent.setComponent(comp);
(5)Intent intent = new Intent(this,OtherActivity.class);
————————————————
二、发送参数与接收参数方式
1、putExtra方式:

发送
intent.putExtra("name","xiazdong");
intent.putExtra("age",20);
接收
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age");

2、Bundle方式:

发送
Bundle bundle = new Bundle();
bundle.putString("name","xiazdong");
bundle.putInt("age",20);
intent.putExtras(bundle);

接收
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");

编写一个程序,可在第一个Activity中输入两个整数,单击“计算”按钮后,在第二个Activity负责求和计算,并将结果返回

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/tex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入数值" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <EditText
android:id="@+id/edt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" /> <EditText
android:id="@+id/edt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout> <Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" /> <TextView
android:id="@+id/tex1"
android:layout_width="300dp"
android:layout_height="50dp"
/> </LinearLayout>

activity_mian.xml 程序

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/e1"
android:layout_width="fill_parent"
android:layout_height="40dp"
/> <TextView
android:id="@+id/e2"
android:layout_width="fill_parent"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/e3"
android:layout_width="fill_parent"
android:layout_height="40dp"
/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回传" />
</LinearLayout>

activity_other.xml

 package com.example.shiyan;

 import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity {
private EditText edt;
private EditText edt1;
private TextView tex1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=(EditText)this.findViewById(R.id.edt);
edt=(EditText)this.findViewById(R.id.edt1);
tex1=(TextView)this.findViewById(R.id.tex1);
Button but=(Button)this.findViewById(R.id.but);
but.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
String number = edt.getText().toString();
String number1 = edt.getText().toString();
Intent intent = new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);
intent.putExtra("shu1",number);
intent.putExtra("shu2",number1);
MainActivity.this.startActivityForResult(intent,100);
}
}); } @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100&&resultCode==200){
String a=data.getStringExtra("sum");
tex1.setText(a); } } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

MainActivity

 package com.example.shiyan;

 import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class OtherActivity extends Activity {
private TextView e1;
private TextView e2;
private TextView e3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
e1=(TextView)this.findViewById(R.id.e1);
e2=(TextView)this.findViewById(R.id.e2);
e3=(TextView)this.findViewById(R.id.e3);
Intent intent=getIntent();
String a=intent.getStringExtra("shu1");
String b=intent.getStringExtra("shu2");
int c=Integer.parseInt(a)+Integer.parseInt(b) ;
String str = String.valueOf(c);
e1.setText(a);
e2.setText(b);
e3.setText(str);
Button b1=(Button)this.findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Intent intent=getIntent();
String a=intent.getStringExtra("shu1");
String b=intent.getStringExtra("shu2");
int c=Integer.parseInt(a)+Integer.parseInt(b) ;
String str = String.valueOf(c);
intent.putExtra("sum",str);
setResult(200,intent);
OtherActivity.this.finish();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.other, menu);
return true;
} }

OtherActivity

安卓开发-Activity-多个Activity的开发方法。的更多相关文章

  1. 【Android Studio】安卓开发初体验2——Activity

    Activity是什么 Activity用于提供可视化用户界面的组件,可以与用户进行交互来完成某项任务,一个应用程序中可以包含零个或多个活动 Activity的创建 首先将左侧的Active Tool ...

  2. 安卓开发笔记——重识Activity

    Activity并不是什么新鲜的东西,老生常谈,这里只是随笔记录一些笔记. 每当说起Activity,感觉最关注的还是它的生命周期,因为要使我们的应用程序更加健壮,客户体验更加良好,如果对生命周期不熟 ...

  3. android开发中关于继承activity类中方法的调用

    android开发中关于继承activity类中的函数,不能在其他类中调用其方法. MainActivity.java package com.example.testmain; import and ...

  4. Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(2)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  5. Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(1)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  6. Android开发学习之路--Activity之初体验

    环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...

  7. Android开发——异步任务中Activity销毁时的问题

    0.  前言 在Android开发中经常会发生Activity的销毁重建,比如用户长时间接听一个电话后回到APP.在Android开发--Fragment知识整理(二)中我们提到了使用Fragment ...

  8. Android开发之漫漫长途 Ⅲ——Activity的显示之Window和View(2)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  9. Activity设置背景透明之开发坑

    Activity设置背景透明的常规方法 方法一.在Manifest.xml中,直接在需要设置的Activity中添加主题样式: Android:theme="@android:style/T ...

  10. Android开发 旋转屏幕导致Activity重建解决方法(转)

     文章来源:http://www.jb51.net/article/31833.htm Android开发文档上专门有一小节解释这个问题.简单来说,Activity是负责与用户交互的最主要机制,任何“ ...

随机推荐

  1. Java学习大纲-0412更新

    非科班报培训班学习Java,从博客园,知乎,CNDS上搜了一圈,暂时按以下计划执行,有问题随时更新--0412 1.培训班的课程按时按点学习完成(毕竟掏钱在学的是不,不好好听亏不亏...) keys: ...

  2. NO.6 ADS1115与MSP432进行I2C通信_运行实例

    B站第一次传视频,手机拍摄大家见谅!

  3. 关于Tensorflow基于Windows安装那些事儿

    声明:代码及博客小白一枚,如有错误,感谢指正~~ 众所周知,摘抄来温习一遍: Tensorflow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes) ...

  4. Vue 百度地图显示规划路线

    Vue 百度地图显示规划路线 1.首选引入相应的文件(建议单页面引入)(如有问题找上一篇博客园) 2.区别就是需要多引入几根不同的文件 import { BaiduMap, BmScale, BmGe ...

  5. (板子)缩点 + DAG上的DP(深搜)luogu P3387

    板子传送门 根据题目意思,我们只需要找出一条点权最大的路径就行了,不限制点的个数.那么考虑对于一个环上的点被选择了,一整条环是不是应该都被选择,这一定很优,能选干嘛不选.很关键的是题目还允许我们重复经 ...

  6. [JavaWeb基础] 016.Struts2 国际化配置

    如果一个软件想要让其受众是全球或者是几个国家的人,那么这个软件就需要支持多种语言,那么我们就需要软件的国际化去对一些文字信息进行国际化处理.web也一样,当外国人打开我们的网站,要是看到满屏幕的中文, ...

  7. 前端和Nodejs的关系 简单理解

    前端使用JS脚本语言进行开发. JS脚本语言需要依赖一个平台运行,从而生成可视化的东西. Node.js提供这个平台,同时提供JS运行需要的一些插件.库.包.轮子.组件.功能等等. JavaScrip ...

  8. Spring boot Sample 012之spring-boot-web-upload

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合web实现文件上传下载 三.步骤 3.1.点击File -> New Project -& ...

  9. 面试题:我们重写一个对象的时候为什么要同时重写hashcode()和equals()方法

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 在创建的类不重写hashCode()和equals() 方法时,默认使用 java 提供的 java.l ...

  10. Java实现 LeetCode 297 二叉树的序列化与反序列化

    297. 二叉树的序列化与反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得 ...