会挽雕弓如满月,西北望,射天狼。这篇文章主要讲述安卓--组建通信相关的知识,希望能为你提供帮助。
实验目的:
熟悉和掌握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 { 12Button bt1; 13Button bt2; 14TextView tv; 15final int SUBACTIVITY1 = 1; 16final int SUBACTIVITY2 = 2; 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21setTitle("MainActivity"); 22bt1 = (Button) findViewById(R.id.bt1); 23bt2 = (Button) findViewById(R.id.bt2); 24tv = (TextView) findViewById(R.id.tv); 25bt1.setOnClickListener(new View.OnClickListener() { 26@Override 27public void onClick(View view) { 28Intent intent = new Intent(MainActivity.this, NewActivity1.class); 29startActivityForResult(intent, SUBACTIVITY1); 30} 31}); 32bt2.setOnClickListener(new View.OnClickListener() { 33@Override 34public void onClick(View view) { 35Intent intent = new Intent(MainActivity.this, NewActivity2.class); 36startActivityForResult(intent, SUBACTIVITY2); 37} 38}); 39} 40 41protected void onActivityResult(int requestCode, int resultCode, Intent data) { 42super.onActivityResult(requestCode, resultCode, data); 43switch (requestCode) { 44case SUBACTIVITY1: 45if (resultCode == 1) { 46Uri uriData = https://www.songbingjia.com/android/data.getData(); 47tv.setText(uriData.toString()); 48} 49case SUBACTIVITY2: 50if (resultCode == -1) { 51Uri uriData = data.getData(); 52tv.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 { 12Button btOk; 13EditText et; 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16super.onCreate(savedInstanceState); 17setContentView(R.layout.activity_new1); 18setTitle("NewActivity1"); 19btOk = (Button)findViewById(R.id.btOK); 20et = (EditText)findViewById(R.id.et); 21btOk.setOnClickListener(new View.OnClickListener() { 22@Override 23public void onClick(View view) { 24String str = et.getText().toString(); 25Uri data = https://www.songbingjia.com/android/Uri.parse("来自NewActivity1的消息" + str); 26Intent result = new Intent(null, data); 27setResult(1, result); 28finish(); 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 { 12Button btOk; 13EditText et; 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16super.onCreate(savedInstanceState); 17setContentView(R.layout.activity_new1); 18setTitle("NewActivity2"); 19btOk = (Button)findViewById(R.id.btOK); 20et = (EditText)findViewById(R.id.et); 21btOk.setOnClickListener(new View.OnClickListener() { 22@Override 23public void onClick(View view) { 24String str = et.getText().toString(); 25Uri data = https://www.songbingjia.com/android/Uri.parse("来自NewActivity2的消息" + str); 26Intent result = new Intent(null, data); 27setResult(-1, result); 28finish(); 29} 30}); 31} 32 }
NewActivity2
文章图片
文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:gravity="center" 8tools:context=".MainActivity" 9android:orientation="vertical"> 10< Button 11android:id="@+id/bt1" 12android:layout_width="match_parent" 13android:layout_height="wrap_content" 14android:layout_weight="0.2" 15android:text="进入NewActivity1!" /> 16 17< Button 18android:id="@+id/bt2" 19android:layout_width="match_parent" 20android:layout_height="wrap_content" 21android:layout_weight="0.2" 22android:text="进入NewActivity2!" /> 23< TextView 24android:id="@+id/tv" 25android:layout_width="match_parent" 26android:layout_height="wrap_content" 27android:layout_weight="0.6" 28android: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" 3android:orientation="vertical" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:gravity="center" > 7 8< LinearLayout 9android:layout_width="match_parent" 10android:layout_height="wrap_content" 11android:orientation="horizontal" 12android:gravity="center" > 13 14< TextView 15android:id="@+id/tv" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:layout_weight="0.2" 19android:text="回信:" /> 20 21< EditText 22android:id="@+id/et" 23android:layout_width="wrap_content" 24android:layout_height="wrap_content" 25android:layout_weight="0.8" /> 26< /LinearLayout> 27 28 29< Button 30android:id="@+id/btOK" 31android:layout_width="match_parent" 32android:layout_height="wrap_content" 33android:text="确定" /> 34 < /LinearLayout>
layout\\activity_new1.xml
文章图片
文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.flyuz.myapplication"> 4 5< application 6android:allowBackup="true" 7android:icon="@mipmap/ic_launcher" 8android:label="@string/app_name" 9android:roundIcon="@mipmap/ic_launcher_round" 10android:supportsRtl="true" 11android:theme="@style/AppTheme"> 12< activity android:name=".MainActivity"> 13< intent-filter> 14< action android:name="android.intent.action.MAIN" /> 15 16< category android:name="android.intent.category.LAUNCHER" /> 17< /intent-filter> 18< /activity> 19 20< activity android:name=".NewActivity1"> 21< intent-filter> 22< action android:name="android.intent.action.VIEW" /> 23 24< category android:name="android.intent.category.DEFAULT" /> 25< /intent-filter> 26< /activity> 27 28< activity android:name=".NewActivity2"> 29< intent-filter> 30< action android:name="android.intent.action.VIEW" /> 31 32< category android:name="android.intent.category.DEFAULT" /> 33< /intent-filter> 34< /activity> 35 36< /application> 37 38 < /manifest>
AndroidManifest.xml 隐式启动:
文章图片
文章图片
1bt.setOnClickListener(new View.OnClickListener() { 2@Override 3public void onClick(View v) { 4Intent intent = new Intent("android.intent.action.VIEW"); //< action android:name="android.intent.action.VIEW" /> 这里的 5Toast.makeText(getApplicationContext(), "隐式启动", Toast.LENGTH_SHORT).show(); 6startActivityForResult(intent, SUBACTIVITY); 7} 8});
MainActivity
推荐阅读
- 安卓--ListView
- Android Monkey使用
- android内部存储与外部存储理解
- T-SQL中的APPLY用法
- Androidadt-bundle 开发环境的搭建_windows
- XML DTD介绍和用法
- TensorFlow中神经网络的实现示例分析
- XML树结构详细解释
- XML属性介绍和用法介绍