新建Activity实现页面之间的跳转与传值。

layout1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9. <Button
  10. android:id="@+id/button"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:text="Button"
  16. app:layout_constraintEnd_toEndOf="parent"
  17. app:layout_constraintStart_toStartOf="parent" />
  18.  
  19. <TextView
  20. android:id="@+id/textView"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_marginBottom="8dp"
  24. android:layout_marginEnd="8dp"
  25. android:layout_marginStart="8dp"
  26. android:layout_marginTop="8dp"
  27. android:text="TextView"
  28. app:layout_constraintBottom_toBottomOf="parent"
  29. app:layout_constraintEnd_toEndOf="parent"
  30. app:layout_constraintStart_toStartOf="parent"
  31. app:layout_constraintTop_toBottomOf="@+id/button" />
  32. </android.support.constraint.ConstraintLayout>

MainActivity1.java

  1. package com.example.aimee.activitytest;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.  
  12. Button button;
  13. TextView textView;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19.  
  20. button=findViewById(R.id.button);
  21. textView=findViewById(R.id.textView);
  22. button.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. Intent intent=new Intent(MainActivity.this,Main2Activity.class);
  26. intent.putExtra("001","android课堂");
  27. startActivityForResult(intent,0x01);//第一个参数intent,第二个参数请求码,这个是有返回值的。
  28.  
  29. }
  30. });
  31. }
  32.  
  33. @Override
  34. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  35. super.onActivityResult(requestCode, resultCode, data);
  36. if(requestCode==0x01&&resultCode==0x02){
  37. int i=data.getIntExtra("002",0);
  38. textView.setText(i+"");
  39. }
  40. }
  41. }

layout2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Main2Activity">
  8.  
  9. <TextView
  10. android:id="@+id/textView2"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:text="TextView"
  16. app:layout_constraintEnd_toEndOf="parent"
  17. app:layout_constraintStart_toStartOf="parent"
  18. tools:layout_editor_absoluteY="108dp" />
  19.  
  20. <Button
  21. android:id="@+id/button2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginBottom="8dp"
  25. android:layout_marginEnd="8dp"
  26. android:layout_marginStart="8dp"
  27. android:layout_marginTop="8dp"
  28. android:text="Button"
  29. app:layout_constraintBottom_toBottomOf="parent"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toTopOf="parent" />
  33. </android.support.constraint.ConstraintLayout>

MainActivity2.java

  1. package com.example.aimee.activitytest;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9.  
  10. public class Main2Activity extends AppCompatActivity {
  11. TextView textView;
  12. Button button;//单击button将数据传回主页面
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main2);
  18. textView=findViewById(R.id.textView2);
  19. button=findViewById(R.id.button2);
  20. final Intent intent=getIntent();//定义intent变量,获取intent里面的值
  21. String data=intent.getStringExtra("001");//因为传入的是字符串,所以是StringExtra
  22. textView.setText(data);
  23. button.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. intent.putExtra("002",888);
  27. setResult(0x02,intent);
  28. finish();//关闭页面
  29. }
  30. });
  31. }
  32. }

第二十七篇-新建Activity的更多相关文章

  1. Android UI开发第二十七篇——实现左右划出菜单

    年前就想写左右滑动菜单,苦于没有时间,一直拖到现在,这篇代码实现参考了网上流行的SlidingMenu,使用的FrameLayout布局,不是扩展的HorizontalScrollView. 程序中自 ...

  2. 第二十七篇 -- QTreeWidget总结

    前言 之前写过几篇关于TreeWidget的文章,不过不方便查阅,特此重新整合作为总结.不过关于QtDesigner画图,还是不重新写了,看 第一篇 就OK. 准备工作 1. 用QtDesigner画 ...

  3. 第二十七篇:Windows驱动中的PCI, DMA, ISR, DPC, ScatterGater, MapRegsiter, CommonBuffer, ConfigSpace

    近期有些人问我PCI设备驱动的问题, 和他们交流过后, 我建议他们先看一看<<The Windows NT Device Driver Book>>这本书, 个人感觉, 这本书 ...

  4. 第二十七篇:SOUI中控件属性查询方法

    SOUI项目的SVN根目录下有一个doc目录,下面有一份控件属性表.包含了大部分控件的大部分属性,不过也不一定完全准确.最保险的办法还是查源代码. SOUI对象包含控件及ISkinObj等从SObje ...

  5. 第二十七篇、使用MVVM布局页面

    思路:架构的设计模式主要有这么两种 >MVC :这种方式用得很多,也很是常见,不在过多的介绍 >MVVM:使用这种 常常需要导入第三方框架,常见的是响应式框架 >主要讲一下ViewM ...

  6. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

  7. flask第二十七篇——九九乘法表

    请关注公众号:自动化测试实战 九九乘法表其实很容易: <table border="1"> <tbody> {% for x in range(1, 10) ...

  8. Python之路【第二十七篇】:web服务器django

    Django 一.web框架 框架,即时framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单的说,就是用别人搭建好的舞台来表演你的才艺 ...

  9. 第二十七篇 玩转数据结构——集合(Set)与映射(Map)

          1.. 集合的应用 集合可以用来去重 集合可以用于进行客户的统计 集合可以用于文本词汇量的统计   2.. 集合的实现 定义集合的接口 Set<E> ·void add(E) ...

随机推荐

  1. PHPWord插件详解

    一下载PHPWorld并配置项目 1.PHPWord框架文件如下: 二使用word模板并使用PHPWord生成doc文件 例如:源代码如下: <?php require_once '../PHP ...

  2. 使用电脑adb给Essential Phone刷机 —(官方篇)

    用ADB给Essential Phone线刷升级 重要:请确保在刷机前已经解锁,关于解锁教程群里有! 准备 原版boot Twrp boot Magisk卡刷包 到官网下载OTA包 准备好Essent ...

  3. How to goproxy

    brew install python python "/users/cuthead/desktop/phuslu-goproxy-9087f35/uploader.py" sel ...

  4. javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint-实体报错

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  5. Windows Server 2012 Hyper-V 快照

    快照 Hyper-V 可提供擷取執行中虛擬機器快照的能力,因此可輕易地回復至前一狀態,對於測試環境相當有幫助. 快照的功用雖然很不錯,不過每次建立快照時都是會消耗相當的硬碟資源,尤其目前的快照點和上一 ...

  6. hdu-2328(暴力枚举+kmp)

    题意:给你n个字符串,问你这n个串的最长公共子串 解题思路:暴力枚举任意一个字符串的所有子串,然后暴力匹配,和hdu1238差不多的思路吧,这里用string解决的: 代码: #include< ...

  7. nginx-添加禁止访问规则

    location ~* /application/(admin|index)/static/.*$ { allow all; } location ~* /(applicaion|addos|coe| ...

  8. kubernetes 基本命令

    查询命令: kubectl get pods -n kube-system kubectl get ClusterRole -n kube-system kubectl get ClusterRole ...

  9. [洛谷P4147] 玉蟾宫

    类型:单调栈 传送门:>Here< 题意:求一个$01$矩阵中最大子矩形(全是$1$)的面积 解题思路 单调栈的一个经典应用 考虑维护一个数组$p[i][j]$表示$(i,j)$往上最多有 ...

  10. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...