Intent--接收返回数据

来源:互联网 发布:圈子圈套 知乎 编辑:程序博客网 时间:2024/05/01 18:39
package org.lxh.demo;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 Receive extends Activity {private TextView show = null ;// 文本显示组件private Button retbut = null ;// 按钮组件@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.receive_main);// 调用默认布局管理器this.show = (TextView) super.findViewById(R.id.show) ;// 取得组件this.retbut = (Button) super.findViewById(R.id.retbut) ;// 取得组件Intent it = super.getIntent() ;// 取得启动此程序的IntentString info = it.getStringExtra("myinfo") ;// 取得设置的附加信息this.show.setText(info) ;// 设置文本显示信息this.retbut.setOnClickListener(new OnClickListenerImpl()) ;// 设置监听}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {Receive.this.getIntent().putExtra("retmsg", "老师:李兴华") ;// 返回信息// 设置返回数据的状态,RESULT_OK与Send.java中的onActivityResult()里判断的对应Receive.this.setResult(RESULT_OK, Receive.this.getIntent()) ;Receive.this.finish() ;// 结束Intent}}}
package org.lxh.demo;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 Send extends Activity {private Button mybut = null ;// 按钮组件private TextView msg = null ;// 文本组件@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.send_main);// 默认布局管理器this.mybut = (Button) super.findViewById(R.id.mybut) ;// 取得组件this.msg = (TextView) super.findViewById(R.id.msg) ;// 取得组件this.mybut.setOnClickListener(new OnClickListenerImpl());// 定义单击事件}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {Intent it = new Intent(Send.this, Receive.class);// 实例化Intentit.putExtra("myinfo", "北京魔乐科技软件学院(www.mldnjava.cn)") ;// 设置附加信息Send.this.startActivityForResult(it, 1);// 启动Activity}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (resultCode) {// 判断操作类型case RESULT_OK:// 成功操作msg.setText("返回的内容是:" + data.getStringExtra("retmsg"));break;case RESULT_CANCELED:// 取消操作msg.setText("操作取消。");break ;default:break;}}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/MyLayout"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/show"android:layout_width="wrap_content"android:layout_height="wrap_content"/> </LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/MyLayout"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/mybut"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按我将跳转到另一个Activity程序"/> </LinearLayout>

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="10" /><application android:icon="@drawable/icon" android:label="@string/app_title"><activity android:name="Send" android:label="@string/app_title"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="Receive" android:label="@string/receive_name" /></application></manifest>


0 0
原创粉丝点击