Intent类的 用法

在Android中Intent类,可以使两个ACtivity之间产生联系,通过了解Intent的基本作用也会了解到多个ACtivity之间的关系。

Intent的用处

  1. 在一个Activity当中启动另外一个ACtivity
  2. 使用Intent在Activity之间传输数据

第一个基础实例程序很简单,第一个Activity有一个按钮,点击后跳转到第二个Activity,并显示文字。如图:

这是第一个ACtivity:

2011053016273798.png

点击按钮后跳转到第二个Activity:

2011053016291069.png

完整代码:

 xml文件:

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
// 第一个Activity的java文件
package geeker.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HelloActivity extends Activity {
private Button myButton = null ;
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);

myButton
= (Button)findViewById(R.id.myButton);

myButton.setText(
" 我的第一个Button " );
myButton.setOnClickListener(
new MyButtonListener());
}
// 内部类
class MyButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
Intent intent
= new Intent();
intent.setClass(HelloActivity.
this , OtherActivity. class );
HelloActivity.
this .startActivity(intent);

}
}
}

// 第二个java文件
package geeker.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {

private TextView myTextView = null ;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView
= (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}

}
ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
//第一个Activity对应的XML文件
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< Button
android:id = "@+id/myButton"
android:layout_width
= "fill_parent"
android:layout_height
= "wrap_content"
/>
</ LinearLayout >



//第二个Activity对应的XML文件
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< TextView
android:id = "@+id/myTextView"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
</ LinearLayout >

先在第一个XML文件中加一个按钮控件

 
  
1 < Button
2 android:id = "@+id/myButton"
3 android:layout_width = "fill_parent"
4 android:layout_height = "wrap_content"
5 />

再在第二个XML文件中添加一个文本显示控件

 
  
1 < TextView
2 android:id = "@+id/myTextView"
3 android:layout_width ="fill_parent"
4 android:layout_height ="wrap_content"
5 />

分别与.java文件产生关联,使得一个Activity对应相应的xml布局文件

该语句通常放到onCreate()函数中,因为每个Activity都必须重写onCreate方法,每个Activity自动生成的时候都会自动调用Oncreate方法,就像构造函数一样。

 
  
1 protected void onCreate(Bundle savedInstanceState) {
2 // TODO Auto-generated method stub
3   super .onCreate(savedInstanceState);
4 setContentView(R.layout.other);
5
6

对控件进行设定:

 
  
1 myButton.setText( " 我的第一个Button " );
2 myButton.setOnClickListener( new MyButtonListener());
3
4 myTextView = (TextView)findViewById(R.id.myTextView);
5 myTextView.setText(R.string.other);

给按钮添加监听器,并且通过Intent对象使得与第二个Activity发生联系

 
  
// 内部类,按钮点击的监听器类
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 新建一个Intent类对象
Intent intent = new Intent();
// 第一个参数是此Activity,第二个是与之联系的Activity
       //setClass就是说从哪个Activity跳转到哪个Activity
intent.setClass(HelloActivity. this , OtherActivity. class );
// 建立好关系之后启动Activity
HelloActivity. this .startActivity(intent);

}

}
// 最后比忘了在Oncreate函数中给BUtton添加这个监听器类对象
myButton.setOnClickListener( new MyButtonListener());

最后还有个容易忘记的地方就是要在Androidjmanifast.xml文件中给第二个Activity进行注册

 
  
< activity android:name =".OtherActivity" android:label ="@string/otherName" />

搞定 !

第二个示例主要体现出Intent在Activity中间数据传送的作用:

程序内容依然很简单:

转载于:https://www.cnblogs.com/liushang0419/archive/2011/05/30/2063449.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值