Android Activity之间通信
2015-12-27 00:51
337 查看
package com.example.myapp; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MyActivity extends Activity { private Button btnOk = null; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnOk = (Button)findViewById(R.id.btnCallOther); //已学安卓两天学到碎片和活动之间通讯 布局,UI和空间 每天都很充实, findViewById 要牢记 很常用 返回的是一个View对象 强制转换成需要的控线 btnOk.setOnClickListener(new MyButtonListener()); // 监听器listener 安卓里很重要的一个机制 和adapter一样 很多动作都需要监听器来实现 } class MyButtonListener implements View.OnClickListener { @Override public void onClick(View view) {//重写onClick方法 Intent intent = new Intent(); // Intent 实现在活动之间转行 。2. 再活动之间传递消息 intent.putExtra("key","value"); //putExtra 把一个程序的活动或者信息传递到下个活动 intent.setClass(MyActivity.this,otherActivity.class); MyActivity.this.startActivity(intent); } } } package com.example.myapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; /** * Created by chang on 14-9-17. */ public class otherActivity extends Activity{ private TextView tv = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); Intent intent = getIntent(); String s = intent.getStringExtra("key"); tv = (TextView)findViewById(R.id.otherTextView); tv.setText(s); } }
相关文章推荐
- Android selector属性
- Android 实现登录注册功能
- 关于android LayoutInflater.inflate()的参数及其用法
- android混合动画实现
- Android属性动画详解(上),初始属性动画的基本用法
- Android_2D绘图的学习Paint,Canvas(一)
- Android shape属性
- android--创建不同的bitmap
- Android项目中使用BeanUtils报异常
- android旋转动画的两种实现方式
- Android Lollipop联系人之PinnedListView简单使用
- 用TextView动态显示文件内容
- Android:Fragment Transition介绍
- android位移动画的两种实现方式
- Android几种layout(布局)的区别
- Android Material Design学习之三TabLayout
- android缩放动画的两种实现方法
- android studio 生成aar并使用
- android导出CSV,中文乱码问题
- android调整屏幕亮度