原文链接: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. Longest Mountain in Array 数组中的最长山脉

    我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...

  2. [ES6系列-03]ES6中关于参数相关特性详解(参数默认值与参数解构赋值与剩余参数)

    [原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 今天总结一下 ES6 中跟参数相关的内容. 欢迎补充斧正.留言交流. 让我们互相学习一起进步. 1. ES6 参数默认值( ...

  3. 泛微 e-cology OA 前台SQL注入

    poc https://github.com/AdministratorGithub/e-cology-OA-SQL 用法:python elog_sql.py http://target 不存在返回 ...

  4. Rocket - diplomacy - enumerateBits

    https://mp.weixin.qq.com/s/KsZqe9W_DM6W6JecK_irvA   介绍AddressSet.enumerateBits方法的实现,主要是x & (-x)的 ...

  5. jchdl - GSL实例 - Add

    https://mp.weixin.qq.com/s/6xcYYdYZTBPTf25xFluzBQ   使用FullAdder级联实现加法器   参考链接: https://github.com/wj ...

  6. Java实现 蓝桥杯 算法提高 因式分解

    算法提高 8-1因式分解 时间限制:10.0s 内存限制:256.0MB 提交此题 问题描述 设计算法,用户输入合数,程序输出若个素数的乘积.例如,输入6,输出23.输入20,输出22*5. 样例 与 ...

  7. Java实现 LeetCode 434 字符串中的单词数

    434. 字符串中的单词数 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my nam ...

  8. Java实现 蓝桥杯 数独游戏

    你一定听说过"数独"游戏. 如图,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独的答案都是唯一 ...

  9. java实现取字母组成串

    ** 取字母组成串** A B C D中取5次,每个字母都可以重复取出,形成一个串. 现在要求,串中A出现的次数必须为偶数(0次也算偶数). 求可以形成多少种可能的串. 参考答案: 528 publi ...

  10. Ansible facts详解

    Ansible是一个系列文章,我会尽量以通俗易懂.诙谐幽默的总结方式给大家呈现这些枯燥的知识点,让学习变的有趣一些. Ansible系列博文直达链接:Ansible入门系列 前言 如果你跟着前面的文章 ...