Android开发之Intent传值实例
Android开发之Intent传值实例
今天我们来探讨一下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);
- }
- }
|
评论暂时关闭