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>
相关文章推荐
- React Native Android 组件IntentAndroid 实现拨打电话
- android利用Intent.ACTION_SEND实现简单分享功能
- Android 使用 intent 实现简单登陆页面
- Android实战简易教程<三>(实现简单绘图组件)
- 【Android】使用Chronometer组件实现一个简单的计时器
- android组件通讯 Intent Filter隐式实现Intent
- Android利用RecognizerIntent识别语音并简单实现打电话动作
- Android笔记---Intent实现Activity跳转
- 移动开发 - Android - 实现两个页面(Activity)的简单跳转(Intent)
- android组件通讯 Intent Filter隐式实现Intent
- Android 四大组件 —— 活动(使用Intent 实现活动的显示跳转)
- Android利用RecognizerIntent识别语音并简单实现打电话动作
- Android四大组件之一ContentProvider实现简单的联系人读取
- android-基础知识:实现简单的拨打电话功能_intent_study
- Android基础之使用Intent实现Activity跳转
- Android:实现最简单的单指移动、双指缩放的图片组件
- android用Intent实现Activity跳转
- Android笔记---Intent实现Activity跳转
- Android 四大组件 —— 活动(使用Intent 实现活动的显示跳转)