安卓开发-Activity-多个Activity的开发方法。
原文链接: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的开发方法。的更多相关文章
- 【Android Studio】安卓开发初体验2——Activity
Activity是什么 Activity用于提供可视化用户界面的组件,可以与用户进行交互来完成某项任务,一个应用程序中可以包含零个或多个活动 Activity的创建 首先将左侧的Active Tool ...
- 安卓开发笔记——重识Activity
Activity并不是什么新鲜的东西,老生常谈,这里只是随笔记录一些笔记. 每当说起Activity,感觉最关注的还是它的生命周期,因为要使我们的应用程序更加健壮,客户体验更加良好,如果对生命周期不熟 ...
- android开发中关于继承activity类中方法的调用
android开发中关于继承activity类中的函数,不能在其他类中调用其方法. MainActivity.java package com.example.testmain; import and ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(2)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(1)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
- Android开发——异步任务中Activity销毁时的问题
0. 前言 在Android开发中经常会发生Activity的销毁重建,比如用户长时间接听一个电话后回到APP.在Android开发--Fragment知识整理(二)中我们提到了使用Fragment ...
- Android开发之漫漫长途 Ⅲ——Activity的显示之Window和View(2)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Activity设置背景透明之开发坑
Activity设置背景透明的常规方法 方法一.在Manifest.xml中,直接在需要设置的Activity中添加主题样式: Android:theme="@android:style/T ...
- Android开发 旋转屏幕导致Activity重建解决方法(转)
文章来源:http://www.jb51.net/article/31833.htm Android开发文档上专门有一小节解释这个问题.简单来说,Activity是负责与用户交互的最主要机制,任何“ ...
随机推荐
- 解决docker创建的elasticsearch-head容器不能连接elasticsearch等问题
在使用docker创建elasticsearch-head容器去连接elasticsearch的时候,容易出两个问题 1.不能连接elasticsearch 修改elasticsearch.yml文件 ...
- Linux,Mac下MySQL的安装及一些知识点的整理
Linux下载安装 在服务器上下载的话,需要安装Mysql5.7相关的yum源 wget https://dev.mysql.com/get/mysql80-community-release-el7 ...
- Eclipse中java文件选中变量名,相同变量都变色显示 .
第一步设置高亮显示的颜色: Window-->preferences-->General-->Editors-->Text Editors-->Annotations-- ...
- html5学习之路_003
html布局 使用<div>元素布局 使用<table>元素布局 <div>元素布局 <!DOCTYPE html> <html> < ...
- 07 . Python3函数
Python3函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().我们可以直接调用 ...
- AVIRIS反射率数据简介
Surface Reflectance 高光谱图像 ↑ AVIRIS高光谱成像光谱仪采集得到的原始图像为辐亮度图像,经过校正后的L1级产品为地表辐亮度信息.但是许多时候,我们更希望知道地面目标物的反射 ...
- 【RT-Thread笔记】OneNet软件包的使用
去年,RT-Thread发布了RT-Thread Studio初版RT-ThreadStudio的使用体验,经过不断更新迭代之后,来到了V1.1.0,咱也来拥抱一下新版本. 本篇笔记咱们以接入OneN ...
- C# Winform退出程序的方法介绍
这篇文章主要介绍了C#中WinForm程序退出方法, 实例总结了技巧退出WinForm程序窗口的各种常用技巧,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中WinForm程序退出方法技巧 ...
- Java实现 LeetCode 35 搜索插入位置
35. 搜索插入位置 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1, ...
- java矩形的关系
在编写图形界面软件的时候,经常会遇到处理两个矩形的关系. 如图[1.jpg]所示,矩形的交集指的是:两个矩形重叠区的矩形,当然也可能不存在(参看[2.jpg]). 两个矩形的并集指的是:能包含这两个矩 ...