Android初级教程
来源:互联网 发布:软件性能测试视频教程 编辑:程序博客网 时间:2024/06/05 13:23
因为我是在写监听网络的时候,不经意想起多级Activity是否可以数据传递(当然再此之前我一般都只是进行A-B的数据传递),因是临时想起,所以包名,XMl名,VIew id并不是很规范。
执行流程:
运行结果:
注意:
Activity注册
<activity android:name=".SecoundActivity"/><activity android:name=".ThirdActivity"/>
代码中的NetState不用处理
Xml中的隐藏View不用处理
MainActivity :
package com.example.dow.receiverdemo;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private TextView mState; private TextView mBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// mState = (TextView) findViewById(R.id.net_state); mBtn = (TextView) findViewById(R.id.mBtn_first); mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,SecoundActivity.class); Bundle bundle = new Bundle(); bundle.putString("data","First数据传递"); intent.putExtras(bundle); Log.e("tag","被点击"); startActivity(intent); } }); }}
SecoundActivity :
package com.example.dow.receiverdemo;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.TextView;/** * Created by YongLiu on 2017/5/24. */public class SecoundActivity extends AppCompatActivity { private TextView mBtn; private String data; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_secound); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); data = (String) bundle.get("data"); Log.e("tag","MainFirst传输过来的数据===="+ data); mBtn = (TextView) findViewById(R.id.mBtn_secound); mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(SecoundActivity.this,ThirdActivity.class); Bundle bundle = new Bundle(); bundle.putString("data",data); intent.putExtras(bundle); startActivity(intent); } }); } }
ThirdActivity :
package com.example.dow.receiverdemo;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;/** * Created by YongLiu on 2017/5/25. */public class ThirdActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); String data = (String) bundle.get("data"); Log.e("tag","Secound传输过来的数据===="+data); }}
MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dow.receiverdemo.MainActivity"> <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/net_state" android:visibility="gone" android:layout_height="wrap_content" android:text="隐藏的网络状态" /> <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/mBtn_first" android:layout_height="wrap_content" android:text="事件多级传递" /></LinearLayout>
SecoundActivity Xml :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/net_state_secound" android:visibility="gone" android:layout_height="wrap_content" android:text="隐藏的网络状态" /> <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/mBtn_secound" android:layout_height="wrap_content" android:text="Secound事件多级传递" /></LinearLayout>
ThirdActivity Xml :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/net_state_three" android:visibility="gone" android:layout_height="wrap_content" android:text="隐藏的网络状态" /> <TextView android:layout_width="match_parent" android:gravity="center" android:padding="5dp" android:id="@+id/mBtn_three" android:layout_height="wrap_content" android:text="Third事件多级传递" /></LinearLayout>
阅读全文
0 0
- Android初级教程
- Android初级教程
- Android初级教程
- Android初级教程
- Android初级教程
- Android初级教程
- Android初级教程
- Android初级教程
- Android程序开发初级教程
- Android程序开发初级教程
- Android Studio开发初级教程
- Android Studio开发初级教程
- Android初级教程人品计算器
- Android初级教程短信防火墙
- Android初级教程实现电话录音
- Android初级教程图片信息
- Android初级教程:屏幕分辨率
- Android初级教程:屏幕分辨率
- 移动web开发
- Class.forName()用法详解
- 开放封闭原则
- J2EE中session的深入理解
- 百度定位SDK出错:unable to resolve static field 155 (SUPPORTED_ABIS) in Landroid/os/Build
- Android初级教程
- Weblogic在Linux上节点服务器启动正常,计算机连接不上
- Android进程间通信(1)-AIDL
- Spring_day02
- 错误:android studio集成第三方库文件,在4.4以下报错:java.lang.NoClassDefFoundError: io.rong.common.SystemUtils问题
- 安信可A7模块 GPRS/GPS使用教程
- java 多线程学习之创建线程
- 高级算法日记3:python数据结构之栈和队列
- spring的multipartResolver和java后端获取的MultipartHttpServletRequest方法对比 (附:遇到的坑)