Android开发之Intent传值实例


今天我们来探讨一下Android的传值问题。

主要实现功能为第一个页面实现信息的填写,在第二个页面实现第一个页面信息的输出

效果图为:

第一个activity实现了对单选、复选、文本框值的获取与传递

  1. ////////////////////UIZuoYeActivity///////////////  
  2.   
  3. //第一个activity  
  4.   
  5. package cn.class3g.activity;  
  6.   
  7.    
  8.   
  9. import android.app.Activity;  
  10.   
  11. import android.content.Intent;  
  12.   
  13. import android.os.Bundle;  
  14.   
  15.    
  16.   
  17. import android.view.View;  
  18.   
  19. import android.view.View.OnClickListener;  
  20.   
  21. import android.widget.Button;  
  22.   
  23. import android.widget.CheckBox;  
  24.   
  25. import android.widget.EditText;  
  26.   
  27. import android.widget.RadioButton;  
  28.   
  29. import android.widget.RadioGroup;  
  30.   
  31. import android.widget.Spinner;  
  32.   
  33.    
  34.   
  35. public class UIZuoYeActivity extends Activity implements OnClickListener {  
  36.   
  37.    /** Called when the activity is first created. */  
  38.   
  39.    RadioGroup rg = null;  
  40.   
  41.    RadioButton manRB = null;  
  42.   
  43.    RadioButton rb = null;  
  44.   
  45.    Button btn = null;  
  46.   
  47.    EditText nameET = null;  
  48.   
  49.    
  50.   
  51.    CheckBox lan, zu, pai, ping;  
  52.   
  53.    
  54.   
  55.    Spinner city;  
  56.   
  57.    
  58.   
  59.    @Override  
  60.   
  61.    public void onCreate(Bundle savedInstanceState) {  
  62.   
  63.       super.onCreate(savedInstanceState);  
  64.   
  65.       setContentView(R.layout.main);  
  66.   
  67.       findView();  
  68.   
  69.    }  
  70.   
  71.    
  72.   
  73.    private void findView() {  
  74.   
  75.       btn = (Button) this.findViewById(R.id.putinId);  
  76.   
  77.       nameET = (EditText) this.findViewById(R.id.nameId);  
  78.   
  79.       manRB = (RadioButton) this.findViewById(R.id.manId);  
  80.   
  81.    
  82.   
  83.       lan = (CheckBox) this.findViewById(R.id.lanId);  
  84.   
  85.       zu = (CheckBox) this.findViewById(R.id.zuId);  
  86.   
  87.       pai = (CheckBox) this.findViewById(R.id.paiId);  
  88.   
  89.       ping = (CheckBox) this.findViewById(R.id.pingId);  
  90.   
  91.    
  92.   
  93.       city = (Spinner) this.findViewById(R.id.cityId);  
  94.   
  95.       btn.setOnClickListener(this);  
  96.   
  97.    }  
  98.   
  99.    
  100.   
  101.    @Override  
  102.   
  103.    public void onClick(View v) {  
  104.   
  105.       // TODO Auto-generated method stub  
  106.   
  107.       // 封装bundle对象  
  108.   
  109.       Bundle bundle = new Bundle();  
  110.   
  111.       // 获取EditText文本框内容  
  112.   
  113.       bundle.putString("name", "用户名称:" + nameET.getText().toString());  
  114.   
  115.       // 获取RadioGroup单选内容  
  116.   
  117.       if (manRB.isChecked()) {  
  118.   
  119.         bundle.putString("sex", "性别:男");  
  120.   
  121.       } else {  
  122.   
  123.         bundle.putString("sex", "性别:女");  
  124.   
  125.       }  
  126.   
  127.       // 获取CheckBox复选框内容  
  128.   
  129.       String temp = "爱好:";  
  130.   
  131.       if (lan.isChecked()) {  
  132.   
  133.         temp += lan.getText().toString();  
  134.   
  135.       }  
  136.   
  137.       if (zu.isChecked()) {  
  138.   
  139.         temp += "";  
  140.   
  141.         temp += zu.getText().toString();  
  142.   
  143.       }  
  144.   
  145.       if (pai.isChecked()) {  
  146.   
  147.         temp += "";  
  148.   
  149.         temp += pai.getText().toString();  
  150.   
  151.       }  
  152.   
  153.       if (ping.isChecked()) {  
  154.   
  155.         temp += "";  
  156.   
  157.         temp += ping.getText().toString();  
  158.   
  159.       }  
  160.   
  161.       bundle.putString("hobby", temp);  
  162.   
  163.       // 获取Spinner下拉菜单内容  
  164.   
  165.       bundle.putString("city", "城市:" + city.getSelectedItem().toString());  
  166.   
  167.    
  168.   
  169.       Intent intent = new Intent(UIZuoYeActivity.this, PutInActivity.class);  
  170.   
  171.       // 传递  
  172.   
  173.       intent.putExtras(bundle);  
  174.   
  175.       startActivity(intent);  
  176.   
  177.    
  178.   
  179.    }  
  180.   
  181. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容