1.介绍

2.相关属性

(1)启动Activity

(2)Intent介绍

(3)关闭Activity

3.多个页面之间传递数据(页面1向页面2传递数据,单向传递数据)

(1)相关属性

注意:data为str的代号

(2)xml文件

第一个界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:id="@+id/tv_weight"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="这是标准体重计算界面"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别" /> <RadioGroup
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"> <RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <TextView
android:id="@+id/tv_userheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入您的身高" /> <EditText
android:id="@+id/et_userheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <Button
android:id="@+id/bt_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
</LinearLayout> </LinearLayout>

第二个界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="这是第二个界面"
android:textSize="30dp" /> <Button
android:id="@+id/bt_goback"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" /> <EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" /> <EditText
android:id="@+id/et_weightValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>

(3)java后台代码

界面1

package com.lucky.test31activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton; public class MainActivity extends AppCompatActivity {
Button bt_calculate;
EditText et_height;
RadioButton radioButton;
String gender; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_calculate=findViewById(R.id.bt_calculate);
et_height=findViewById(R.id.et_userheight);
radioButton=findViewById(R.id.radioButton); bt_calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现页面之间的跳转
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//若页面之间要传递数据,可以用putExtra()方法,若不需要传递数据则下面两句可以不写
//toString()一定不能少
if(radioButton.isChecked()){
gender="男";
}else {
gender="女";
}
intent.putExtra("003",gender);
intent.putExtra("004",Integer.parseInt(et_height.getText().toString()));
startActivity(intent); //进行页面跳转
}
});
}
}

界面2

package com.lucky.test31activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class SecondActivity extends AppCompatActivity {
Button button1;
EditText et_message;
EditText et_userweight;
int userheight;
double userweight;
String gender; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button1=findViewById(R.id.bt_goback);
et_message=findViewById(R.id.et_message);
et_userweight=findViewById(R.id.et_weightValue);
Intent intent=getIntent();//获取Intent
String name=intent.getStringExtra("001");
String pwd=intent.getStringExtra("002");
gender=intent.getStringExtra("003");
userheight=intent.getIntExtra("004",0);
if(gender.equals("男")){
userweight=(userheight-80)*0.7;
}else {
userweight=(userheight-70)*0.6; }
et_message.setText("用户名为:"+name+"\n密码为:"+pwd);
et_userweight.setText("您的体重为:"+userweight); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();//关闭页面
}
});
}
}

4.页面之间传递数据(页面1与页面2双向传递数据)

(1)介绍

(2) java 后台代码

主界面

package com.lucky.test32activity2;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends AppCompatActivity {
EditText page1output;
EditText frompage2;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1output=findViewById(R.id.editText_page1output);
frompage2=findViewById(R.id.editText_frompage2);
button1=findViewById(R.id.button_page1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,page2Activity.class);//实例化intent
intent.putExtra("001",page1output.getText().toString()); //设置所传递的数据
startActivityForResult(intent,0x01); //参数2为请求码
}
}); } @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//判断请求码与结果码是否一致
if(requestCode==0x01&&resultCode==0x02){
frompage2.setText(data.getStringExtra("002"));
}
}
}

第二界面

package com.lucky.test32activity2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class page2Activity extends AppCompatActivity { EditText page1input;
EditText page2return;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
page1input=findViewById(R.id.editText_page1input);
page2return=findViewById(R.id.editText_page2return);
button2=findViewById(R.id.button_page2);
final Intent intent=getIntent();
page1input.setText(intent.getStringExtra("001"));
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent(); //实例化intent
intent1.putExtra("002",page2return.getText().toString()); //设置数据,toString()不能省略
setResult(0x02,intent1); //从第二界面返回第一界面
finish(); //关闭页面
}
}); }
}

5.效果图

动态1对应工程名:test31,动态2对应工程名:test32

      

Android 多个界面(Activity)的更多相关文章

  1. Android系统编程入门系列之加载界面Activity

    上回说到应用初始化加载及其生命周期,在Android系统调用Applicaiton.onCreate()之后,继续创建并加载清单文件中注册的首个界面即主Activity,也可称之为入口界面.主Acti ...

  2. Android系统编程入门系列之界面Activity绘制展示

    上篇文章介绍了界面Activity的启动方式和生命周期,本篇将继续介绍在界面Activity中的内容是如何绘制展示给用户的. 在Android系统上运行新创建的界面Activtiy,给用户展示的是空白 ...

  3. Android系统编程入门系列之界面Activity交互响应

    在上篇文章中已经了解到界面Activity的绘制完全依赖其加载的视图组件View,不仅如此,用户的每次触摸操作都可以在界面Activity内接收并响应,也可以直接传递给其中的某个视图View响应.本文 ...

  4. Android系统编程入门系列之界面Activity响应丝滑的传统动画

    上篇文章介绍了应用程序内对用户操作响应的相关方法位置,简单的响应逻辑可以是从一个界面Activity跳转到另一个界面Activity,也可以是某些视图View的相对变化.然而不管是启动一个界面执行新界 ...

  5. Android 数据传递(一) Activity之间的数据传递

    bundle Google Bundle类说明 Bundle类是一个key-value对.Activity之间的数据通信可以通过bundle类来实现数据的存储.即将数据放入bundle里面,将Bund ...

  6. Android四大组件之Activity(活动)及其布局的创建与加载布局

    Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ...

  7. Android四大组件之——Activity的生命周期(图文详解)

        转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai       联系方式:JohnTsai.Work@gmail.com       [Andro ...

  8. android学习笔记26——Activity

    Activity ==> android中四大组件:Activity.Service.BroadcastReceiver.ContentProvider Activity组件用于对用户呈现操作界 ...

  9. Android Service实时向Activity传递数据

    演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示. 步骤如下:1.新建一个android项目工程,取名为demo ...

随机推荐

  1. omitTermFreqAndPositions设置,词频FQ在打分中默认为1

    compressed=true|false,是否使用gzip压缩(只有TextField和StrField可以压缩) compressThreshold=压缩阀值 multiValued=true|f ...

  2. 利用Chrome的Performance工具排查页面性能问题(原叫timeline)

    当页面中发生卡顿,最先考虑的是swf文件造成的卡顿,经过排查发现不是swf造成的影响,利用Chrome的Performance工具发现页面中的一些元素不断在重新布局,造成潜在的性能瓶颈. 首先在Chr ...

  3. qt数据库有效插件为空的情况

    打了一周的环境,从ubuntu到win7,搭建环境的时间比写代码的时间都多.先简单的介绍一下我搭建的环境不是纯QT环境,是一个芬兰的软件开发商开发出来的SDK里面完全融合qt,其中qt是以单独的目录存 ...

  4. Python学习笔记_操作Excel

    Python 操作Exel,涉及下面几个库: 1.xlrd 读取Excel文件 2.xlwt 向Excel文件写入,并设置格式 3.xlutils 一组Excel高级操作工具,需要先安装xlrd和xl ...

  5. solr第二天 京东案例 课程文档 有用

    全文检索技术   Lucene&Solr               Part3 1. 课程计划 1. Solr配置中文分析器 a) Schema.xml的配置 b) 配置IKAnalyzer ...

  6. java基础面试题 背过1

    web.xml文件中可以配置哪些内容? 答:web.xml用于配置Web应用的相关信息,如:监听器(listener) ContextLoaderListener .过滤器(filter) Strut ...

  7. jQuery--加一行减一行

    效果: 知识点: 克隆--clone() 追加--append() 移除--remove() 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...

  8. VC中多线程(一)Win32 API对多线程编程的支持

    http://blog.sina.com.cn/s/blog_4ae08ad801008yer.html

  9. Robot Framework - 常用断言讲解

    RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便:不能停留在知道或者是会得程度,只有熟练使用各关键字,才能提升自动化用例的写作效率. 下面将逐个 ...

  10. Django-Web框架之创建项目和应用

    Django我们是基于python3来演示的.首先我们来安装一下django框架.使用pip3 install django安装的是最新的版本: 我们在pycharm中创建django工程.如图所示: ...