移动应用开发 实验报告5 Intent回传数据
一、实验目的 掌握startActivityForResult()、onActivityResult()等方法; 使用Intent实现不同activity之间的数据双向传输。 |
二、实验内容 使用按钮事件与Intent实现页面数据的双向传输 |
四、实验代码及结果 package com.example.luz1;
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 MainActivity extends Activity { private Button mybut = null; private TextView msg = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mybut = (Button) super.findViewById(R.id.mybut); this.mybut.setOnClickListener((OnClickListener) new OnClickListenerImpl()); this.msg = (TextView) super.findViewById(R.id.msg); }
private class OnClickListenerImpl implements OnClickListener{ @Override public void onClick(View v){ Intent it = new Intent(MainActivity.this, receive.class); it.putExtra("myinfo","你传你马呢?"); MainActivity.this.startActivityForResult(it,1); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ switch(resultCode){ case RESULT_OK: MainActivity.this.msg.setText("返回的内容是:"+data.getStringExtra("retmsg")); break; case RESULT_CANCELED: MainActivity.this.msg.setText("操作取消"); default: break; } } }
package com.example.luz1;
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 protected 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.rebut); Intent it = super.getIntent(); String info = it.getStringExtra("myinfo"); this.show.setText(info); this.retbut.setOnClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener{ @Override public void onClick(View v){ receive.this.getIntent().putExtra("retmsg","你要的马吗?"); receive.this.setResult(RESULT_OK,receive.this.getIntent()); receive.this.finish(); }
} }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_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:id="@+id/mybut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="传个马" /> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/mybut" android:layout_alignRight="@+id/mybut" android:layout_below="@+id/mybut" android:layout_marginTop="30dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/rebut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="回传个马"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.luz1" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.luz1.MainActivity" android:label="Luz1" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.luz1.receive" android:label="Luz2" > </activity> </application>
</manifest>
|
结果:
|
五、试验分析(思考) 首先,要打开Activity,先创建意图对象。 第一个参数是intent。 这个方法有两个重载的方法,一个是两个参数,一个是三个参数。 那我们就把三个参数说完吧: 第二个参数是requestCode,第三个参数 * @param options Additional options for how the Activity should be started. 这个参数的意思是设置Activity是如何启动的,在ActivityOptions这个类里面,就是封装Activity的启动参数的。这个Bundle可以理解为一个可以封装多种集合即可。
|