Android(java)学习笔记164:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)
2024-09-08 14:15:29
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); } }); } }
记得在AndroidMainfest.xml中注册SmsActivity,如下:
<?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 如下:
activity_sms.xml :
<?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。
最新文章
- LINQ to SQL语句(17)之对象加载
- gedit 乱码问题
- session放入缓存(redis)、DB
- paip.java c# .net php python调用c++ c dll so windows api 总结
- [转]YII2 常用数据库操作
- Java常见排序算法之归并排序
- python截取中文字符串
- laravel--模型中各种属性详解
- 【USACO 1.4.4】母亲的牛奶
- php设计模式2策略模式
- ognl中的#、%和$
- CA认证
- 安卓组件-BroadcastReceiver
- mysql日期函数 当前日期 curdate() , 当前年 year(curdate()), 取date的年份 year(date) ,取date的月份 month(date)
- menu菜单项和menubutton菜单按钮的结合使用
- C#实现冲顶大会辅助工具 (截图+图像识别+搜索)
- 01-01_环境准备_pyenv
- JavaIO 总结
- Oracle最新的Sql笔试题及答案
- 【Flask】abort和errorhandler、app_errorhandler进行请求中断及自定义异常处理
热门文章
- 棋盘问题(dp)
- bzoj 4289 TAX —— 点边转化
- 微信小程序在线制作 自己制作微信小程序
- Codeforces - Gym102028 - 2018 Jiaozuo Regional Contest
- 《对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们》的问题的解决方法
- JSP | 基础 | 新建Hello world 的三种方式
- SQL 实战语句(9)
- C#中this指针的用法示例
- AJPFX总结java开发常用类(包装,数字处理集合等)(二)
- SpringBoot 2.x (8):模板引擎