Android开发基础:Intent与Bundle的使用,实现页面跳(2)

   @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);
 }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjjjw.html