安卓–组建通信

实验目的:

熟悉和掌握Android组件间通信的方式和技巧。

实验要求:

1. 运行课本的示例程序,理解组件通信的方式和过程

2.设计一个主Activity和一个子Activity(Sub-Activity),使用主Activity上的按钮启动子Activity,并将子Activity的一些信息返回给主Activity,并显示在主Activity上。

 

技术分享图片

技术分享图片
技术分享图片

 1 package com.flyuz.myapplication; 2  3 import android.content.Intent; 4 import android.net.Uri; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.TextView;10 11 public class MainActivity extends AppCompatActivity {12  Button bt1;13  Button bt2;14  TextView tv;15 final int SUBACTIVITY1 = 1;16 final int SUBACTIVITY2 = 2;17  @Override18 protected void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20  setContentView(R.layout.activity_main);21 setTitle("MainActivity");22 bt1 = (Button) findViewById(R.id.bt1);23 bt2 = (Button) findViewById(R.id.bt2);24 tv = (TextView) findViewById(R.id.tv);25 bt1.setOnClickListener(new View.OnClickListener() {26  @Override27 public void onClick(View view) {28 Intent intent = new Intent(MainActivity.this, NewActivity1.class);29  startActivityForResult(intent, SUBACTIVITY1);30  }31  });32 bt2.setOnClickListener(new View.OnClickListener() {33  @Override34 public void onClick(View view) {35 Intent intent = new Intent(MainActivity.this, NewActivity2.class);36  startActivityForResult(intent, SUBACTIVITY2);37  }38  });39  }40 41 protected void onActivityResult(int requestCode, int resultCode, Intent data) {42 super.onActivityResult(requestCode, resultCode, data);43 switch (requestCode) {44 case SUBACTIVITY1:45 if (resultCode == 1) {46 Uri uriData = data.getData();47  tv.setText(uriData.toString());48  }49 case SUBACTIVITY2:50 if (resultCode == -1) {51 Uri uriData = data.getData();52  tv.setText(uriData.toString());53  }54  }55  }56 }

MainActivity

技术分享图片
技术分享图片

 1 package com.flyuz.myapplication; 2  3 import android.net.Uri; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.content.Intent;10 11 public class NewActivity1 extends AppCompatActivity {12  Button btOk;13  EditText et;14  @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17  setContentView(R.layout.activity_new1);18 setTitle("NewActivity1");19 btOk = (Button)findViewById(R.id.btOK);20 et = (EditText)findViewById(R.id.et);21 btOk.setOnClickListener(new View.OnClickListener() {22  @Override23 public void onClick(View view) {24 String str = et.getText().toString();25 Uri data = Uri.parse("来自NewActivity1的消息" + str);26 Intent result = new Intent(null, data);27 setResult(1, result);28  finish();29  }30  });31  }32 }

NewActivity1

技术分享图片
技术分享图片

 1 package com.flyuz.myapplication; 2  3 import android.net.Uri; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.content.Intent;10 11 public class NewActivity2 extends AppCompatActivity {12  Button btOk;13  EditText et;14  @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17  setContentView(R.layout.activity_new1);18 setTitle("NewActivity2");19 btOk = (Button)findViewById(R.id.btOK);20 et = (EditText)findViewById(R.id.et);21 btOk.setOnClickListener(new View.OnClickListener() {22  @Override23 public void onClick(View view) {24 String str = et.getText().toString();25 Uri data = Uri.parse("来自NewActivity2的消息" + str);26 Intent result = new Intent(null, data);27 setResult(-1, result);28  finish();29  }30  });31  }32 }

NewActivity2

技术分享图片
技术分享图片

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3  xmlns:app="http://schemas.android.com/apk/res-auto" 4  xmlns:tools="http://schemas.android.com/tools" 5  android:layout_width="match_parent" 6  android:layout_height="match_parent" 7  android:gravity="center" 8  tools:context=".MainActivity" 9  android:orientation="vertical">10 <Button11 android:id="@+id/bt1"12  android:layout_width="match_parent"13  android:layout_height="wrap_content"14  android:layout_weight="0.2"15  android:text="进入NewActivity1!" />16 17 <Button18 android:id="@+id/bt2"19  android:layout_width="match_parent"20  android:layout_height="wrap_content"21  android:layout_weight="0.2"22  android:text="进入NewActivity2!" />23 <TextView24 android:id="@+id/tv"25  android:layout_width="match_parent"26  android:layout_height="wrap_content"27  android:layout_weight="0.6"28  android:text="" />29 30 </LinearLayout>

layout\activity_main.xml

技术分享图片
技术分享图片

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3  android:orientation="vertical" 4  android:layout_width="match_parent" 5  android:layout_height="match_parent" 6  android:gravity="center" > 7  8 <LinearLayout 9 android:layout_width="match_parent"10  android:layout_height="wrap_content"11  android:orientation="horizontal"12  android:gravity="center" >13 14 <TextView15 android:id="@+id/tv"16  android:layout_width="wrap_content"17  android:layout_height="wrap_content"18  android:layout_weight="0.2"19  android:text="回信:" />20 21 <EditText22 android:id="@+id/et"23  android:layout_width="wrap_content"24  android:layout_height="wrap_content"25  android:layout_weight="0.8" />26 </LinearLayout>27 28 29 <Button30 android:id="@+id/btOK"31  android:layout_width="match_parent"32  android:layout_height="wrap_content"33  android:text="确定" />34 </LinearLayout>

layout\activity_new1.xml

 

相关文章