Intent跳转
2016-05-02 20:02
435 查看
一、Intent分为两类:
(1)显示Intent,通过名字指定目标组件。显式的intent用于程序内部消息——如:Activity启动一个下属服务或启动一个姊妹Activity。(2)隐式的Intent,没有指定目标,Android系统需要查找最适合处理intent的组件(或几个组件)——一个单一的Activity。通过把intent对象的内容和intent管理器比较,判断那个组件是潜在的接收者。过滤器提供组件的能力并且划定它可以处理的intent。它开启可以接收隐式intent的组件类型。如果组件没有intent过滤器,它仅仅可以接收显式的intent。含有过滤器的组件既可以接收隐式intent也可以接收显式intent。
二、传递数据
(1) 通过Bundle打包批量传数据Bundle bundle=new Bundle();
bundle.putString("name", name);
bundle.putInt("age", 16);
intent.putExtras(bundle)
(2)使用intent.putExtra("key", value)一个个传输数据。
package com.example.sayhellotoyou; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etName; private static final String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName=(EditText)findViewById(R.id.etName); } @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; } public void sayHello(View view){ String name = etName.getText().toString(); Intent intent =new Intent(); /*intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel://5556"));*/ /*intent.setClass(this, ShowActivity.class);*/<pre class="html" name="code">package com.example.sayhellotoyou; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ShowActivity extends Activity { private TextView tvName; private String name; private int age; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); getData(); initViews(); bindData(); } private void bindData() { // TODO Auto-generated method stub tvName.setText("名字是"+name+"年龄是"+age+"。"); } private void initViews() { // TODO Auto-generated method stub tvName =(TextView)findViewById(R.id.tvName); } private void getData() { // TODO Auto-generated method stub /*Bundle bundle=getIntent().getExtras(); name=bundle.getString("name"); age=bundle.getInt("age");*/ name=getIntent().getStringExtra("name"); age=getIntent().getIntExtra("age",12); }}
相关文章推荐
- 值传递 & 引用传递
- javascript入门篇之事件处理
- handler消息传递机制
- Python Pandas库 常见使用错误总结
- HDU 2586 LCA转RMQ
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />解释
- 经典SQL练习题4----面试题及相关知识
- [Interactive Python]Memory游戏 Mini-project #5
- 使用$this变量访问方法和属性
- LTE小区搜索-物理小区ID和同步信号PSS、SSS
- 欧拉函数
- C++中的栈和队列操作明细
- JDBC笔记-BLOB和获取主键
- leetcode笔记—Find the Duplicate Number
- HDOJ-2066-一个人的旅行
- redis-集合
- 经典SQL练习题3----student/score/cource/teacher
- HDU 5677 ztr loves substring
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation (贪心)
- Spring之集合DI