1.首先我们看看下面这个需求:
这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到"短信内容"显示。这里要实现A界面和B界面数据互相通信。
2.实现上面需求,通过案例演示方法逻辑:
(1)新建一个Android工程,命名为"短信助手",首先设置activity_main.xml布局文件如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.himi.Smshelper.MainActivity" > 7 8 <Button 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="浏览选择短信" 12 android:onClick="select_Sms" /> 13 14 <EditText 15 android:id="@+id/et_Sms" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:lines="6" 19 android:inputType="textMultiLine" 20 /> 21 22 </LinearLayout>
(2)接下来,我们修改MainActivity.java,代码如下:
1 package com.himi.Smshelper; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 9 public class MainActivity extends Activity { 10 private EditText ed_Sms; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 ed_Sms = (EditText) findViewById(R.id.et_Sms); 17 18 19 Intent intent = getIntent(); 20 String context = intent.getStringExtra("context"); 21 22 ed_Sms.setText(context); 23 24 25 } 26 27 28 public void select_Sms(View view) { //按钮点击事件,点击按钮开启新的界面SmsActivity界面 29 Intent intent = new Intent(this, SmsActivity.class); 30 //直接打开新的界面 影响返回键,点击返回键只能返回上一个页面(不能直接退出) 用户体验不好 31 startActivity(intent); 32 } 33 34 }
这里当我们点击这个MainActivity界面上的按钮的时候,就会转而开启SmsActivity界面;
(3)SmsActivity代码如下:
1 package com.himi.Smshelper; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.AdapterView; 8 import android.widget.AdapterView.OnItemClickListener; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class SmsActivity extends Activity { 13 private ListView lv; 14 private String[] sms = { 15 "七夕节到了,送你一碗长寿面,祝你们的爱情像长寿面一样长长久久,永远不分离。送你一份酸辣汤,让你们生活像酸辣汤一样有滋有味。真诚的祝福你七夕快乐。", 16 "雪花的美丽,飘舞着心情的惦记,圣诞节最思念是你,给你我祝福的深意,把幸福累积,祈祷着祝愿的真挚,圣诞节祝你万事如意!", 17 "三年光阴,匆匆而过,如梦的年纪,弥漫着串串欢声笑语,不要挥手叹息,觉得繁花尽去,鼓足勇气,不要忘了互递惊喜的消息。", 18 "亲爱的织女:七夕情人节将至,愿我们高举中国特色痴情主义伟大旗帜,发扬鹊桥相会优良传统,保持二人世界爱情在线,携手开创爱情新局面。牛郎敬上。" 19 20 }; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO 自动生成的方法存根 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_sms); 26 lv = (ListView) findViewById(R.id.iv); 27 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item, sms)); 28 29 30 //给listview的条目设置点击事件 31 lv.setOnItemClickListener(new OnItemClickListener() { 32 33 public void onItemClick(AdapterView<?> parent, View view, 34 int position, long id) { 35 36 String context = sms[position]; 37 Intent intent = new Intent(SmsActivity.this, MainActivity.class); 38 intent.putExtra("context", context); 39 //直接打开新的界面 影响返回键,点击返回键返回上一个页面(不能直接退出),用户体验不好 40 startActivity(intent); 41 42 } 43 44 }); 45 46 } 47 48 }
记得在AndroidMainfest.xml中注册SmsActivity,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.himi.Smshelper" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="15" 9 android:targetSdkVersion="17" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 26 <activity 27 android:name="com.himi.Smshelper.SmsActivity"> 28 29 </activity> 30 </application> 31 32 </manifest>
(4)这里的activity_sms.xml 和 item.xml 如下:
activity_sms.xml :
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/iv" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 </ListView> 12 13 </LinearLayout>
item.xml :
1 <?xml version="1.0" encoding="utf-8"?> 2 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:textColor="#AA000000" 6 android:textSize="15sp" > 7 8 9 </TextView>
(5)运行效果如下:
当我们点击" 浏览选择短信 ",如下:
当我们点击第一个条目时候,如下:
(6)小结:
上面程序基本上完成了,两个界面直接的互相通信;
但是还是存在BUG,就是每当我们点击返回键,就会推出到上一次打开的界面,我们一直点多次返回键才能推出程序,这是因为我们直接每次都是startActivity这样不断开启新的页面,但是我们并没有关闭。
这样的用户体验是不好的,所以我们这里就需要优化这个BUG,下一讲会说明如何解决这个BUG。