Android开发–UI之Bundle的使用

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



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

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

第一个界面的代码:

package com.example.intenttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private EditText edtname;
private EditText edtage;
private EditText edtsex;
private Button send; private String name;
private String age;
private String sex; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); edtname = (EditText)findViewById(R.id.edt1);
edtage = (EditText)findViewById(R.id.edt2);
edtsex = (EditText)findViewById(R.id.edt3);
send = (Button)findViewById(R.id.btn); send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
startTransmitData();
}
}); } private void startTransmitData(){
name = edtname.getText().toString(); //将edtname强制转换为String类型
age = edtage.getText().toString();
sex = edtsex.getText().toString(); //将edtsex强制转换为String类型
Intent mIntent = new Intent();
mIntent.setClass(getApplicationContext(),ShowActivity.class);
Bundle mBundle = new Bundle();
//通过key--valuse进行记录
mBundle.putString("name", name);
mBundle.putString("age", age);
mBundle.putString("sex", sex);
//将整个的数据进行封装到intent中,等待传递
mIntent.putExtras(mBundle);
startActivity(mIntent); } }

第一个界面的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" > </EditText> <EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" > </EditText> <EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" > </EditText> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="发送" /> </LinearLayout>

第二个界面的代码:

package com.example.intenttest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class ShowActivity extends Activity{ private TextView txtname;
private TextView txtage;
private TextView txtsex;
private Button cal; private String name;
private String age;
private String sex; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showactivity); txtname = (TextView)findViewById(R.id.txt1);
txtage = (TextView)findViewById(R.id.txt2);
txtsex = (TextView)findViewById(R.id.txt3); Intent inten = getIntent();
Bundle bundle = inten.getExtras();
//通过key值进行获取所对应的valuse值
name = bundle.getString("name");
age = bundle.getString("age");
sex = bundle.getString("sex");
//将内容显示在控件中
txtname.setText(name);
txtage.setText(age);
txtsex.setText(sex); cal = (Button)findViewById(R.id.cal); cal.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
finish();
}
}); } }

第二个界面的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" > <TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="测试1" /> <TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="测试2" /> <TextView
android:id="@+id/txt3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="测试3" /> <Button
android:id="@+id/cal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回" /> </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. Eclipse全屏及插件下载

    Eclipse全屏插件下载 解压下载的压缩包,将  plugins  文件夹中的  cn.pande.eclipsex.fullscreen_1.0.7.jar  文件拷贝到Eclipse安装目录下的 ...

  2. centos中忘记root密码问题

    centos中root密码问题   几次在虚拟机中安装cenos,都没有提示输入root密码,具体操作如下: 1.在启动的时候,进入启动界面后,按除了Enter键之外的任意键,即可进入该界面.然后 按 ...

  3. poj2723

    把每对钥匙看做一个变量,那两个钥匙看做他的两个状态 每一个开门的要求就是一个条件(xi or xj) 很显然有了2sat的基本要素 2sat是一个判定性问题,而这题求最多能过几个门: 不难想到二分答案 ...

  4. puppy 制作linux

    经过一段时间的使用以后,我们每个人电脑里的Puppy Linux都是独一无二的,我们可以通过简单的方法将自己电脑上的Puppy制作成iso或Live-CD,成为自己玩的“Only You”Puppy ...

  5. Android的string-array数据源简单使用

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

  6. Servlet3.0学习总结(三)——基于Servlet3.0的文件上传

    在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...

  7. 使用haproxy做负载均衡时保持客户端真实的IP

    haproxy里添加设置项 option forwardfor option httpclose apache的日志格式修改 LogFormat "MY IP=%{X-Forwarded-F ...

  8. VS.NET2010水晶报表安装部署

    水晶报表VS2010版IDE安装标准版SAP Crystal Reports, version for Visual Studio 2010 - Standard: 下载地址: http://down ...

  9. 关注LoadRunner脚本回放日志中的Warning信息-转载

    关注LoadRunner脚本回放日志中的Warning信息   最近在与大家的讨论中发现了LoadRunner的很多问题,出于解决问题的出发点,我也就相关自己不理解的问题在Google中搜索了一番,并 ...

  10. [codevs1287]矩阵乘法

    题目描述 Description 小明最近在为线性代数而头疼,线性代数确实很抽象(也很无聊),可惜他的老师正在讲这矩阵乘法这一段内容.当然,小明上课打瞌睡也没问题,但线性代数的习题可是很可怕的.小明希 ...