您的位置:首页 > 移动开发 > Android开发

Android 中Intent组件实现简单的Activity跳转

2014-04-08 15:43 148 查看
<!-- Manifest.xml -->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.muye.intent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="13" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
  <activity
android:name="com.muye.intent.Send"
android:label="@string/send_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.muye.intent.Receive"
android:label="@string/receive_name">

</activity>
</application>

</manifest>

/**
*
* Send.java
*/

package com.muye.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;
import android.widget.TextView;

public class Send extends Activity {

private Button mybut;
private TextView msg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
this.mybut=(Button)super.findViewById(R.id.mybut);
this.msg=(TextView)super.findViewById(R.id.msg);
this.mybut.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Send.this,Receive.class);
intent.putExtra("myinfo", "send发过去的内容");
Send.this.setIntent(intent);
Send.this.startActivityForResult(intent, 1);//如果要从其他的Activity中返回数据到本Activity中,要用这个方法
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send, menu);
return true;
}
@Override//从第二个Activity返回数据到第一个Activity


//最重要的是int resultCod这个参数。从第二个Activity返回数据到本Activity的结果状态码 protected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch(resultCode){case RESULT_OK:Send.this.msg.setText("返回的内容是:"+data.getStringExtra("retmsg"));break;case
RESULT_CANCELED:Send.this.msg.setText("操作取消");break;default:break;}}
}


<!-- activity_receive.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Send" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myview"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回数据到send"
android:id="@+id/retbut"
/>

</LinearLayout>


/**
* Receive.java    Activity
*/

package com.muye.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;
import android.widget.TextView;

public class Receive extends Activity {

private TextView myview;
private Button but;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Receive.this.myview=(TextView)super.findViewById(R.id.myview);
Receive.this.but=(Button)super.findViewById(R.id.retbut);

this.but.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Receive.this.getIntent().putExtra("retmsg", "我是****");
Receive.this.setResult(RESULT_OK, Receive.this.getIntent());
Receive.this.finish();
}//从这个Activity返回数据到第一个Activity,就要设置Intent返回的结果状态,并结束当前Activity.
});
Intent intent=super.getIntent();
String info=intent.getStringExtra("myinfo");
Receive.this.myview.setText(info);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send, menu);
return true;
}

}




<!-- strings.xml -->

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Intent</string>
<string name="action_settings">Settings</string>
<string name="mybutname">Send activity</string>
<string name="send_name">send name</string>
<string name="receive_name">receive name</string>

</resources>


<!-- activity_send.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Send" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mybutname"
android:id="@+id/mybut"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/msg"/>
</LinearLayout>








                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: