2010年2月8日星期一

包裹(Bundle)

包裹(Bundle)

包裹(Bundle)是字符串值(String value)對各種可包裹類型的映射(Mapping). 在活動(Activity)之間的數據傳遞就往往用上包裹(Bundle).

修改變先前的應用程序"活動(activity)和意圖(intent)", 以使用包裹傳遞數據.



修改主佈局(main.xml), 添加一個編輯文本(EditText).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your Name?"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/inamefield"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch Activity2"
android:id="@+id/launchbutton"
/>
</LinearLayout>


修改主活動代碼, 使用Intent.putExtra()傳遞數據給Activity2.
package com.AndroidIntent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidIntent extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button launchButton = (Button)findViewById(R.id.launchbutton);
final EditText iNameField = (EditText)findViewById(R.id.inamefield);
launchButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(AndroidIntent.this, Activity2.class);
newIntent.putExtra("name", iNameField.getText().toString());
startActivity(newIntent);
}});
}
}


修改layout2.xml, 添加一個文本視圖(TextView).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's Activity2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/onamefield"
/>
</LinearLayout>


修改Activity2代碼, 使用getIntent().getExtras()獲得數據, 並顯示在文本視圖(TextView)中.
package com.AndroidIntent;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
TextView oNameField = (TextView)findViewById(R.id.onamefield);

Bundle extras = getIntent().getExtras();
oNameField.setText("Hello " + extras.getString("name"));
}
}


記住, 需要修改清單文件(AndroidManifest.xml)加入ctivity2, 參考"活動(activity)和意圖(intent)".

相關文章: startActivityForResult 和 onActivityResult



沒有留言:

發佈留言