Android学习之Intent接收返回数据
send_main.xml:
<?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"> <Button android:id="@+id/mybut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按我将跳转到另一个Activity程序"/> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
receive_main.xml:
<?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"> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/retbut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回数据到Send。"/> </LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_title">Intent操作</string> <string name="send_name">发送Intent的Activity程序。</string> <string name="receive_name">接收Intent的Activity程序。</string> </resources>
AndroidManifest.xml:
<?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>
Send.java:
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 ; // 文本组件 @Override public 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 { @Override public void onClick(View view) { Intent it = new Intent(Send.this, Receive.class); // 实例化Intent it.putExtra("myinfo", "我的发送信息测试") ; // 设置附加信息 Send.this.startActivityForResult(it, 1); // 启动Activity } } @Override protected 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; } } }
Receive.java:
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 ; // 按钮组件 @Override public 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() ; // 取得启动此程序的Intent String info = it.getStringExtra("myinfo") ; // 取得设置的附加信息 this.show.setText(info) ; // 设置文本显示信息 this.retbut.setOnClickListener(new OnClickListenerImpl()) ; // 设置监听 } private class OnClickListenerImpl implements OnClickListener { @Override public 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 } } }
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30