本例子使用import android.os.Bundle对象进行数据封装,点击按钮时将数据传递到另一个Activity进行显示。
1、主界面xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:" />
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/male">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣:" />
<CheckBox
android:id="@+id/sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
<CheckBox
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"/>
<CheckBox
android:id="@+id/reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书"/>
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
2、java代码:
public class MainActivity extends Activity {
private EditText et_name ;
private EditText et_number;
private RadioButton rb_male;
//private RadioButton rb_female;
private CheckBox cb_sport;
private CheckBox cb_music;
private CheckBox cb_read;
private Button btn_submit;
public void init()
{
et_name = (EditText)findViewById(R.id.name);
et_number = (EditText)findViewById(R.id.number);
rb_male = (RadioButton)findViewById(R.id.male);
//rb_female = (RadioButton)findViewById(R.id.female);
cb_sport = (CheckBox)findViewById(R.id.sport);
cb_read = (CheckBox)findViewById(R.id.reading);
cb_music = (CheckBox)findViewById(R.id.music);
btn_submit = (Button)findViewById(R.id.submit);
btn_submit.setOnClickListener(new ButtonListener());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public class ButtonListener implements OnClickListener
{
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
if(et_name.getText().toString()==null || et_name.getText().toString().length()<1)
{
Toast.makeText(MainActivity.this, "姓名不能为空", Toast.LENGTH_LONG).show();
return;
}
if(et_number.getText().toString()==null || et_number.getText().toString().length()<1)
{
Toast.makeText(MainActivity.this, "学号不能为空", Toast.LENGTH_LONG).show();
return;
}
sb.append("您的姓名:").append(et_name.getText().toString()).append("\n")
.append("学号:").append(et_number.getText().toString()).append("\n")
.append("性别:").append(rb_male.isChecked()?"男":"女").append("\n")
.append("爱好:").append(cb_sport.isChecked()?"运动 ":"").append(cb_music.isChecked()?"音乐":"").append (cb_read.isChecked()?"阅读 ":"");
Intent intent = new Intent();
intent.setClass(MainActivity.this, AnotherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("data", sb.toString());
intent.putExtras(bundle);
startActivity(intent);
MainActivity.this.finish();
}
}
}
3、另一Activity的界面只包含一个TextView控件来显示传递过来的数据。(略)
4、另一个Activity接收数据的java代码:
public class AnotherActivity extends Activity {
private TextView tv_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
Bundle b = getIntent().getExtras();
String str = b.getString("data");
tv_data = (TextView)findViewById(R.id.data);
tv_data.setText(str);
}
}
5、配置AndroidManifest.xml
<activity
android:name="com.czh.bundledemo.AnotherActivity"
android:label="@string/app_name" >
</activity>
6、运行结果: