IntentTest
来源:互联网 发布:axure8.0汉化包 mac 编辑:程序博客网 时间:2024/06/11 19:43
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="张三" android:background="#0000aa" /><TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="21" android:background="#00aa00" /> <Button android:id="@+id/enter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始传值"/></LinearLayout>
othermain.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/name1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="等待传值" android:background="#0000aa" /><TextView android:id="@+id/age1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="等待传值" android:background="#00aa00" /> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回值"/></LinearLayout>
package com.hyz;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 IntentTest extends Activity {private TextView name = null;private TextView age = null;private Button enter = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (TextView) findViewById(R.id.name); age = (TextView) findViewById(R.id.age); enter = (Button)findViewById(R.id.enter); enter.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("Name", name.getText().toString()); bundle.putString("Age", age.getText().toString()); intent.putExtras(bundle); intent.setClass(IntentTest.this, OtherIntent.class); // startActivity(intent); startActivityForResult(intent, 1); } }); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if(requestCode==1){if(resultCode==2)setTitle("Cancel****");elseif(resultCode==3){Bundle bundle1 = new Bundle();bundle1 = data.getExtras();age.setText(bundle1.getString("age"));}}}}
package com.hyz;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 OtherIntent extends Activity {private TextView name = null;private TextView age = null;private Button back = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.othermain); name = (TextView) findViewById(R.id.name1); age = (TextView) findViewById(R.id.age1); back = (Button)findViewById(R.id.back); Bundle bundle = getIntent().getExtras(); name.setText(bundle.getString("Name")); back.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("age", "传回来了一个age"); intent.putExtras(bundle); setResult(3,intent); finish(); } }); }}