您的位置:首页 > 移动开发 > Android开发

Android(java)学习笔记221:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

2015-09-10 12:37 876 查看
1.首先我们看看下面这个需求:





这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到"短信内容"显示。这里要实现A界面和B界面数据互相通信。

2.实现上面需求,通过案例演示方法逻辑:

(1)新建一个Android工程,命名为"短信助手",首先设置activity_main.xml布局文件如下:

 <LinearLayout 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:orientation="vertical"
tools:context="com.himi.Smshelper.MainActivity" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浏览选择短信"
android:onClick="select_Sms" />

<EditText
android:id="@+id/et_Sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="6"
android:inputType="textMultiLine"
/>

</LinearLayout>


(2)接下来,我们修改MainActivity.java,代码如下:

 package com.himi.Smshelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
private EditText ed_Sms;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_Sms = (EditText) findViewById(R.id.et_Sms);

Intent intent = getIntent();
String context = intent.getStringExtra("context");

ed_Sms.setText(context);

}

public void select_Sms(View view) { //按钮点击事件,点击按钮开启新的界面SmsActivity界面
Intent intent = new Intent(this, SmsActivity.class);
//直接打开新的界面   影响返回键,点击返回键只能返回上一个页面(不能直接退出) 用户体验不好
startActivity(intent);
}

}


这里当我们点击这个MainActivity界面上的按钮的时候,就会转而开启SmsActivity界面;

(3)SmsActivity代码如下:

 package com.himi.Smshelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SmsActivity extends Activity {
private ListView lv;
private String[] sms  = {
"七夕节到了,送你一碗长寿面,祝你们的爱情像长寿面一样长长久久,永远不分离。送你一份酸辣汤,让你们生活像酸辣汤一样有滋有味。真诚的祝福你七夕快乐。",
"雪花的美丽,飘舞着心情的惦记,圣诞节最思念是你,给你我祝福的深意,把幸福累积,祈祷着祝愿的真挚,圣诞节祝你万事如意!",
"三年光阴,匆匆而过,如梦的年纪,弥漫着串串欢声笑语,不要挥手叹息,觉得繁花尽去,鼓足勇气,不要忘了互递惊喜的消息。",
"亲爱的织女:七夕情人节将至,愿我们高举中国特色痴情主义伟大旗帜,发扬鹊桥相会优良传统,保持二人世界爱情在线,携手开创爱情新局面。牛郎敬上。"

};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
lv = (ListView) findViewById(R.id.iv);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item, sms));

//给listview的条目设置点击事件
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

String context = sms[position];
Intent intent = new Intent(SmsActivity.this, MainActivity.class);
intent.putExtra("context", context);
//直接打开新的界面   影响返回键,点击返回键返回上一个页面(不能直接退出),用户体验不好
startActivity(intent);

}

});

}

}


[b]记得在AndroidMainfest.xml中注册[b]SmsActivity,如下:[/b][/b]

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.himi.Smshelper"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.himi.Smshelper.SmsActivity">

</activity>
</application>

</manifest>


(4)这里的activity_sms.xml 和 item.xml 如下:

[b]activity_sms.xml :[/b]

 <?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" >

<ListView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</LinearLayout>


item.xml :

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#AA000000"
android:textSize="15sp" >

</TextView>


(5)运行效果如下:





当我们点击" 浏览选择短信 ",如下:





当我们点击第一个条目时候,如下:





(6)小结:

上面程序基本上完成了,两个界面直接的互相通信;

但是还是存在BUG,就是每当我们点击返回键,就会推出到上一次打开的界面,我们一直点多次返回键才能推出程序,这是因为我们直接每次都是startActivity这样不断开启新的页面,但是我们并没有关闭。

这样的用户体验是不好的,所以我们这里就需要优化这个BUG,下一讲会说明如何解决这个BUG。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: