Activity与Intent
来源:互联网 发布:sybase数据库win7 编辑:程序博客网 时间:2024/05/17 22:16
主要内容:
1.多个Activity之间的关系
2.Intent的基本作用
3.在一个Activity当中启动另外一个Activity的方法
4.使用Intent在Activity之间传递数据的基本方法
多个Activiry之间的关系:
如上图所示,两个Activity要进行通信,就需要其中一个Activity使用startActivity方法来和另一个进行沟通。
Intent的基本作用:
一个Intent对象包含了一组信息:
1.Componet name
2.Action
3.Data
3.Category
4.Extras
6.Flags
下面了解下Intent的简单使用方法:
1.创建新的layout文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Intent" android:textColor="#FF0000"/> </LinearLayout>主要就是显示一个TextView。
2.创建新的activity:
右键->new -> class
代码如下:
package com.shanl.intent;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other_activity);}}主要就是显示layout中的内容
3.注册activity:
在AndroidManifest.xml中加入:
<activity android:name="com.shanl.intent.OtherActivity" android:label="OtherActivity"></activity>进行注册
4.列出第一个activity的src:
package com.shanl.intent;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button)findViewById(R.id.button);buttonListener listener = new buttonListener();button.setOnClickListener(listener);}class buttonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(MainActivity.this, OtherActivity.class);MainActivity.this.startActivity(intent);}}@Overridepublic 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;}}在监听器中生成Intent对象,设置跳转的activity,天转:
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
效果如下:
按下按钮后:
如何使用Intent在activity之间传递数据?
class buttonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(MainActivity.this, OtherActivity.class);intent.putExtra("abc", "123");MainActivity.this.startActivity(intent);}}在监听器中调用:intent.putExtra("abc", "123");传进一个键值对abc,它的值为123
在另一个activity中接受键值对:
protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other_activity);textView = (TextView)findViewById(R.id.text);Intent intent = getIntent();String value = intent.getStringExtra("abc");textView.setText(value);}Intent intent = getIntent();
String value = intent.getStringExtra("abc");
textView.setText(value);
注意:Intent可以在不是同一个应用程序中的activity间传递数据,比如开启一个系统自带的短信发送功能的activity:
在监听器中加入下面代码即可启动短信activity:
Uri uri = Uri.parse("smsto://0800000123"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri);intent.putExtra("sms_body", "The SMS text"); startActivity(intent);
- Intent与Activity介绍
- Activity与Intent
- Intent与Activity
- Activity与Intent
- Activity与Intent
- Activity与Intent
- Intent与Activity
- Activity与Intent
- Activity与intent
- Intent 与 Activity 相互关系
- android 开发 Activity 与intent
- Android Activity与Intent机制
- 如何认识activity与intent
- activity与intent的关系
- Activity与Intent(一)
- android中的Activity与Intent
- Activity 与 Intent, 开启另一个activity ,用Intent传值
- Activity Task 与 Intent Filter Flag
- WPF进阶之接口(2):IDisposable,ICollectionView
- 提供几个处理图片内存溢出的方法
- java文件名排序
- php中magic_quotes_gpc的作用
- WPF进阶之接口(3):INotifyPropertyChanged,ICommand
- Activity与Intent
- Dns服务的简单配置
- Windows Phone 下载网络图片
- WPF进阶之接口(4):ICommand实现详解
- Java反射详解
- QT 实现上下文菜单
- Java 执行对象构造函数理解
- 电脑常识
- 二进制注册表数据应该怎样读取?