今天我們來探討一下android的傳值問題。
主要實現功能爲第一個頁面實現信息的填寫,在第二個頁面實現第一個頁面信息的輸出
效果圖爲:
第一個activity實現了對單選、複選、文本框值的獲取與傳遞
////////////////////UIZuoYeActivity///////////////
//第一個activity
package cn.class3g.activity;
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.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class UIZuoYeActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
RadioGroup rg = null;
RadioButton manRB = null;
RadioButton rb = null;
Button btn = null;
EditText nameET = null;
CheckBox lan, zu, pai, ping;
Spinner city;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
private void findView() {
btn = (Button) this.findViewById(R.id.putinId);
nameET = (EditText) this.findViewById(R.id.nameId);
manRB = (RadioButton) this.findViewById(R.id.manId);
lan = (CheckBox) this.findViewById(R.id.lanId);
zu = (CheckBox) this.findViewById(R.id.zuId);
pai = (CheckBox) this.findViewById(R.id.paiId);
ping = (CheckBox) this.findViewById(R.id.pingId);
city = (Spinner) this.findViewById(R.id.cityId);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 封裝bundle對象
Bundle bundle = new Bundle();
// 獲取EditText文本框內容
bundle.putString("name", "用戶名稱:" + nameET.getText().toString());
// 獲取RadioGroup單選內容
if (manRB.isChecked()) {
bundle.putString("sex", "性別:男");
} else {
bundle.putString("sex", "性別:女");
}
// 獲取CheckBox複選框內容
String temp = "愛好:";
if (lan.isChecked()) {
temp += lan.getText().toString();
}
if (zu.isChecked()) {
temp += "";
temp += zu.getText().toString();
}
if (pai.isChecked()) {
temp += "";
temp += pai.getText().toString();
}
if (ping.isChecked()) {
temp += "";
temp += ping.getText().toString();
}
bundle.putString("hobby", temp);
// 獲取Spinner下拉菜單內容
bundle.putString("city", "城市:" + city.getSelectedItem().toString());
Intent intent = new Intent(UIZuoYeActivity.this, PutInActivity.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name" />
<EditText
android:id="@+id/nameId"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sex" />
<RadioGroup
android:id="@+id/sexId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/manId"
android:orientation="horizontal" >
<RadioButton
android:id="@id/manId"
android:text="男" />
<RadioButton
android:id="@+id/womanId"
android:text="女" />
</RadioGroup>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/city" />
<Spinner
android:id="@+id/cityId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/城市"
android:prompt="@string/city" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/hobby" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<CheckBox
android:id="@+id/lanId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="籃球" />
<CheckBox
android:id="@+id/zuId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
</TableRow>
</TableLayout>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<CheckBox
android:id="@+id/paiId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排球" />
<CheckBox
android:id="@+id/pingId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/putinId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/putin" />
</RelativeLayout>
</LinearLayout>
第二個activity實現了對第一個界面輸入內容的獲取,獲取到內容後,然後List迭代,用ListView在第二個界面上顯示出來
//////////////////PutInActivity///////////////////
//第二個activity
package cn.class3g.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class PutInActivity extends Activity {
ListView listView = null;
Bundle bundle;
// TextView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.putin);
listView = (ListView) findViewById(R.id.listId);
// 接收
bundle = this.getIntent().getExtras();
List<String> list = new ArrayList<String>();
list.add(bundle.getString("name"));
list.add(bundle.getString("sex"));
list.add(bundle.getString("city"));
list.add(bundle.getString("hobby"));
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(Adapter);
Button btn = (Button) this.findViewById(R.id.returnId);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
});
}
}
//////////////////////putin.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title" />
<ListView
android:id="@+id/listId"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/returnId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout>