Android开发–UI之Bundle的使用

最近,把之前学过的东西大体的整理了以下,并且想把学过的心得分享给大家。我自己做了一个小小的demo,以便说明具体的应用。



这里的两个界面是通过第一个界面输入,然后,第二个界面输出结果的。

废话少说,直接进入正题。

第一个界面的代码:

  1. package com.example.intenttest;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class MainActivity extends Activity {
  10. private EditText edtname;
  11. private EditText edtage;
  12. private EditText edtsex;
  13. private Button send;
  14. private String name;
  15. private String age;
  16. private String sex;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. edtname = (EditText)findViewById(R.id.edt1);
  22. edtage = (EditText)findViewById(R.id.edt2);
  23. edtsex = (EditText)findViewById(R.id.edt3);
  24. send = (Button)findViewById(R.id.btn);
  25. send.setOnClickListener(new OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. startTransmitData();
  29. }
  30. });
  31. }
  32. private void startTransmitData(){
  33. name = edtname.getText().toString(); //将edtname强制转换为String类型
  34. age = edtage.getText().toString();
  35. sex = edtsex.getText().toString(); //将edtsex强制转换为String类型
  36. Intent mIntent = new Intent();
  37. mIntent.setClass(getApplicationContext(),ShowActivity.class);
  38. Bundle mBundle = new Bundle();
  39. //通过key--valuse进行记录
  40. mBundle.putString("name", name);
  41. mBundle.putString("age", age);
  42. mBundle.putString("sex", sex);
  43. //将整个的数据进行封装到intent中,等待传递
  44. mIntent.putExtras(mBundle);
  45. startActivity(mIntent);
  46. }
  47. }

第一个界面的xml代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8. <EditText
  9. android:id="@+id/edt1"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:ems="10" >
  13. </EditText>
  14. <EditText
  15. android:id="@+id/edt2"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:ems="10" >
  19. </EditText>
  20. <EditText
  21. android:id="@+id/edt3"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:ems="10" >
  25. </EditText>
  26. <Button
  27. android:id="@+id/btn"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_marginTop="10dp"
  31. android:text="发送" />
  32. </LinearLayout>

第二个界面的代码:

  1. package com.example.intenttest;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class ShowActivity extends Activity{
  10. private TextView txtname;
  11. private TextView txtage;
  12. private TextView txtsex;
  13. private Button cal;
  14. private String name;
  15. private String age;
  16. private String sex;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.showactivity);
  22. txtname = (TextView)findViewById(R.id.txt1);
  23. txtage = (TextView)findViewById(R.id.txt2);
  24. txtsex = (TextView)findViewById(R.id.txt3);
  25. Intent inten = getIntent();
  26. Bundle bundle = inten.getExtras();
  27. //通过key值进行获取所对应的valuse值
  28. name = bundle.getString("name");
  29. age = bundle.getString("age");
  30. sex = bundle.getString("sex");
  31. //将内容显示在控件中
  32. txtname.setText(name);
  33. txtage.setText(age);
  34. txtsex.setText(sex);
  35. cal = (Button)findViewById(R.id.cal);
  36. cal.setOnClickListener(new OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. finish();
  40. }
  41. });
  42. }
  43. }

第二个界面的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. <TextView
  7. android:id="@+id/txt1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:textColor="#000"
  11. android:text="测试1" />
  12. <TextView
  13. android:id="@+id/txt2"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:textColor="#000"
  17. android:text="测试2" />
  18. <TextView
  19. android:id="@+id/txt3"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:textColor="#000"
  23. android:text="测试3" />
  24. <Button
  25. android:id="@+id/cal"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:text="返回" />
  29. </LinearLayout>

代码到这里已经完成了大部分的功能,只是需要在配置文件中进行以下配置就可以了。

上面的解释很简单也很明了,我相信,你看过之后一定会有很大帮助吧。

你学会了吗??

Android开发--UI之Bundle的使用的更多相关文章

  1. Android开发 UI布局

    Android开发 UI布局一.线性布局LinearLayout 什么是线性布局? 其实呢,线性布局就是把所有的孩子摆在同一条线上 <?xml version="1.0" e ...

  2. Android开发UI之开源项目第一篇——个性化控件(View)篇

    原文:http://blog.csdn.net/java886o/article/details/24355907 本文为那些不错的Android开源项目第一篇——个性化控件(View)篇,主要介绍A ...

  3. Android开发——进程间通信之Bundle和文件

    0.  前言 不论是Android还是其他操作系统,都会有自己的IPC机制,所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是很不同的概念,线程是CPU ...

  4. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  5. Android 开发UI牛博[转]

    Android 新兴的UI模式——侧边导航栏 侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个 ...

  6. Android开发UI之EditText+DatePicker带日期选择器的编辑框

    1. 声明EditText变量,并关联到相应控件上 private EditText sellStartTime; private EditText sellEndTime; sellStartTim ...

  7. Android开发UI之Action Bar

    郭大神的讲解:http://blog.csdn.net/guolin_blog/article/details/18234477 官网链接:http://developer.android.com/i ...

  8. Android开发UI之去掉title bar

    去掉屏幕上的title bar有3个方法: 1.java代码实现: @Override publicvoid onCreate(Bundle savedInstanceState) { super.o ...

  9. Android开发UI之Toast的使用

    Toast,A toast provides simple feedback about an operation in a small popup. 对于操作提供一个简单反馈信息. 官网链接:htt ...

随机推荐

  1. linux 内核驱动--Platform Device和Platform_driver注册过程

    linux 内核驱动--Platform Device和Platform_driver注册过程 从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Pla ...

  2. .NET(C#)调用webService获取客户端IP地址所属区域(非异步)

    功能描述: 此接口用于获取客户端访问的IP的地址所属的区域(国家,城市等).通过输入IP地址查询国家.城市.所有者等信息.没有注明国家的为中国输入参数:IP地址(自动替换 " ." ...

  3. [HDU 1565+1569] 方格取数

    HDU 1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. IIS里面网站停止了,不能启动

    IIS里面网站文件夹显示红色的叉叉,停止了,不能启动,所有站点都停止了: 原来是Word wide web publish service 服务停止了,启动就好了

  5. Spring_Springmvc_mybatis一般配置

    web.xml配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=&quo ...

  6. Jni 类型转换接口

    1.JString char*互相转化 char* JstringToChar(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstr ...

  7. (一)Redis初学教程之安装篇

    1.下载windows下Redis服务安装程序(有32位的和64位的,识操作系统安装) 下载地址:https://github.com/dmajkic/redis/downloads 2.安装教程(详 ...

  8. 2016/9/7 jdbc.properties配置数据库相关

    ##MySQL#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/test#jdbc.username=ro ...

  9. jQuery语法基础&选择器

    jQuery 语法 jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作. 基础语法是:$(selector).action() 美元符号定义 jQuery 选择符(select ...

  10. 20169210《Linux内核原理与分析》第十一周作业

    第17章 设备与模块 关于设备驱动和设备管理,讨论四种内核成分. 设备类型:在所有的linux系统中为了统一普遍设备的操作所分的类. 模块:Linux内核中用于按需加载和卸载目标码的机制. 内核对象: ...