Android开发基础:Intent与Bundle的使用,实现页面跳转及信息传

Mexican 2011-07-27

Intent 与 Bundle 是Android开发中非常基础和常用的知识,今天我们就用他们两个组合实现一个页面跳转并传递数据的例子。

step 1 :

新建android工程,创建两个Activity:Activity01(原Activity)

                                                          Activity02(跳转后Activity)

并且要在AndroidManifest.xml中声明Activity02

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity01"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  
<activity android:name="Activity02"></activity>
    </application>

step 2:

分别创建两个Activity对应的XML(布局文件)

 

step 3:

编写代码

 

工程结构图如下所示:

悲剧呀,不能上传图片。。

不过大家应该都懂的

 

那就上代码吧,很简单:

           这是原Activity01的代码:

package yzy.cxt.com;

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.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Activity01 extends Activity {
 
 //定义控件变量
 private RadioButton radioButton01;
 private RadioButton radioButton02;
 private EditText editText;
 private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // 绑定变量
        radioButton01 = (RadioButton)findViewById(R.id.radiobutton01);
        radioButton02 = (RadioButton)findViewById(R.id.radiobutton02);
        editText = (EditText)findViewById(R.id.edit);
        button = (Button)findViewById(R.id.button);
        
        // 为radioButton01设置监听事件
        radioButton01.setOnCheckedChangeListener(new OnCheckedChangeListener () {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    radioButton01.setChecked(isChecked);
    if (radioButton02.isChecked()) {
     radioButton02.setChecked(false);
    }
   }
         
        });
       
        // 为radioButton02设置监听事件
        radioButton02.setOnCheckedChangeListener(new OnCheckedChangeListener () {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    radioButton02.setChecked(isChecked);
    if (radioButton01.isChecked()) {
     radioButton01.setChecked(false);
    }
   }
         
        });
       
        // 为Button设置监听事件
        button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    String sexType = "";
    String descri = "";
    // 获取信息
    if (radioButton01.isChecked() && !radioButton02.isChecked()) {
     sexType = "man";
    } else if (radioButton02.isChecked() && !radioButton01.isChecked()) {
     sexType = "woman";
    } else {
     Toast.makeText(Activity01.this, "请正确选择性别", Toast.LENGTH_LONG);
    }
    
    descri = editText.getText().toString();
    
    // 将信息放入Bundle
    Bundle bundle = new Bundle ();
    bundle.putString("sexType", sexType);
    bundle.putString("desc", descri);
    
    Intent intent = new Intent();
    intent.setClass(Activity01.this, Activity02.class);
    intent.putExtras(bundle);
    
    // 开始。。。。跳转
    startActivity(intent);
   }
         
        });
    }
}

    这是迁移到的activity02的代码:

package yzy.cxt.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Activity02 extends Activity{

 private TextView message;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity02layout);
  
  Bundle bundle = this.getIntent().getExtras();
  String sex = "";
  // 接收信息并显示
  message = (TextView)findViewById(R.id.message);
  String sexType = bundle.getString("sexType");
  String desc = bundle.getString("desc");
  
  // 判断
  if ("man".equals(sexType)) {
   sex = "男的";
  } else {
   sex = "女的";
  }
  
  message.setText("你是" + sex + ", 你帅到" + desc);
 }

}

相关推荐