life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
http://www.cnblogs.com/salam/archive/2010/10/27/1862730.html

效果如下:

 

初始界面

输入信息

“确定”以后

 

一、API文档说明

  1.介绍

    用于不同Activity之间的数据传递

  1.重要方法

    clear():清除此Bundle映射中的所有保存的数据。

    clone():克隆当前Bundle

    containsKey(String key):返回指定key的值

    getString(String key):返回指定key的字符

    hasFileDescriptors():指示是否包含任何捆绑打包文件描述符

    isEmpty():如果这个捆绑映射为空,则返回true

    putString(String key, String value):插入一个给定key的字符串值

    readFromParcel(Parcel parcel):读取这个parcel的内容

    remove(String key):移除指定key的值

    writeToParcel(Parcel parcel, int flags):写入这个parcel的内容

二、实例

  

public class BundleDemo extends Activity {
 private EditText etName;
 Button btn;

 /*
  * (non-Javadoc)
  *
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  setContentView(R.layout.bundle);

  etName = (EditText) findViewById(R.id.etname);
  btn = (Button) findViewById(R.id.btn);
  btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    String info = etName.getText().toString();
    Bundle bundle = new Bundle();

  //保存输入的信息
    bundle.putString("name", info);
    Intent intent=new Intent(BundleDemo.this,BundleDemo1.class);
   intent.putExtras(bundle);
   finish();
   startActivity(intent);
   }
  });

 }

}

 

public class BundleDemo1 extends Activity {
private TextView etName;
 /* (non-Javadoc)
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.b1);
  
  etName=(TextView)findViewById(R.id.txtname);
  Bundle b=getIntent().getExtras();
  //获取Bundle的信息
  String info=b.getString("name");
  etName.setText("您的姓名:"+info);
 }

}

 

三、与SharedPreferences的区别

  SharedPreferences是简单的存储持久化的设置,就像用户每次打开应用程序时的主页,它只是一些简单的键值对来操作。它将数据保存在一个xml文件中

  Bundle是将数据传递到另一个上下文中或保存或回复你自己状态的数据存储方式。它的数据不是持久化状态。

标签: Android2.2

一、使用Intent

    在 Android 中,不同的 Activity 实例可能运行在一个进程中,也可能运行在不同的进程中。因此我们需要一种特别的机制帮助我们在 Activity 之间传递消息。Android 中通过 Intent 对象来表示一条消息,一个 Intent 对象不仅包含有这个消息的目的地,还可以包含消息的内容,这好比一封 Email,其中不仅应该包含收件地址,还可以包含具体的内容。对于一个 Intent 对象,消息“目的地”是必须的,而内容则是可选项。

在上面的实例中通过 Activity. startActivity(intent)启动另外一个 Activity 的时候,我们在 Intent 类的构造器中指定了“收件人地址”。

如果我们想要给“收件人”Activity 说点什么的话,那么可以通过下面这封“e-mail”来将我们消息传递出去:

 

  1. Intent intent =new Intent(CurrentActivity.this,OtherActivity.class);  
  2.   // 创建一个带“收件人地址”的 email    
  3.  Bundle bundle =new Bundle();// 创建 email 内容   
  4.  bundle.putBoolean("boolean_key"true);// 编写内容   
  5.  bundle.putString("string_key""string_value");   
  6.  intent.putExtra("key", bundle);// 封装 email    
  7.  startActivity(intent);// 启动新的 Activity   
 

 

那么“收件人”该如何收信呢?在 OtherActivity类的 onCreate()或者其它任何地方使用下面的代码就可以打开这封“e-mail”阅读其中的信息:

 

  1. Intent intent =getIntent();// 收取 email    
  2.  Bundle bundle =intent.getBundleExtra("key");// 打开 email    
  3.  bundle.getBoolean("boolean_key");// 读取内容   
  4.  bundle.getString("string_key");  
 

 

上面我们通过 bundle对象来传递信息,bundle维护了一个 HashMap<String, Object>对象,将我们的数据存贮在这个 HashMap 中来进行传递。但是像上面这样的代码稍显复杂,因为 Intent 内部为我们准备好了一个 bundle,所以我们也可以使用这种更为简便的方法:

 

  1. Intent intent =new Intent(EX06.this,OtherActivity.class);   
  2.  intent.putExtra("boolean_key"true);   
  3.  intent.putExtra("string_key""string_value");   
  4.  startActivity(intent);  
 

 

接收:

 

  1. Intent intent=getIntent();   
  2. intent.getBooleanExtra("boolean_key",false);   
  3. intent.getStringExtra("string_key");   
 

 

二、使用SharedPreferences 

    SharedPreferences 使用 xml 格式为 Android 应用提供一种永久的数据存贮方式。对于一个 Android 应用,它存贮在文件系统的/data/ data/your_app_package_name/shared_prefs/目录下,可以被处在同一个应用中的所有 Activity 访问。Android 提供了相关的 API 来处理这些数据而不需要程序员直接操作这些文件或者考虑数据同步问题。

 

  1. // 写入 SharedPreferences    
  2.  SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);   
  3.  Editor editor = preferences.edit();   
  4.  editor.putBoolean("boolean_key"true);   
  5.  editor.putString("string_key""string_value");   
  6.  editor.commit();   
  7.           
  8.  // 读取 SharedPreferences    
  9.  SharedPreferences preferences = getSharedPreferences("name", MODE_PRIVATE);   
  10.  preferences.getBoolean("boolean_key"false);   
  11.  preferences.getString("string_key""default_value");   
 

 

三、其他方式

Android 提供了包括 SharedPreferences 在内的很多种数据存贮方式,比如 SQLite,文件等,程序员可以通过这些 API 实现 Activity 之间的数据交换。如果必要,我们还可以使用 IPC 方式。

 

http://blog.csdn.net/sdlgxxy/article/details/6226127
posted on 2012-02-22 10:34 life02 阅读(651) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理