Android学习笔记之Bundle
http://www.2cto.com/kf/201201/117907.html
1.介绍
用于不同Activity之间的数据传递
2.重要方法
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的内容
Android2Activity.java
package Android2.test;
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 Android2Activity extends Activity {
private EditText et = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText)findViewById(R.id.edit);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
public class ButtonListener implements OnClickListener{
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString("Name", et.getText().toString());
Intent intent=new Intent(Android2Activity.this,bundle.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"/>
</LinearLayout>
bundle.java
package Android2.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class bundle extends Activity{
private TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
text = (TextView)findViewById(R.id.text);
text.setText(bundle.getString("Name"));
}
}
bundle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>