关闭

Acitivty间数据的传递

149人阅读 评论(0) 收藏 举报
分类:
MainActivity.java
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void click(View v){
    	Intent intent = new Intent(this, SecondActivity.class);
    	//把数据封装至intent对象中
//    	intent.putExtra("malename", "李志");
//    	intent.putExtra("femalename", "芙蓉姐姐");
    	
    	//把数据封装至bundle对象中
    	Bundle bundle = new Bundle();
    	bundle.putString("malename", "李志");
    	bundle.putString("femalename", "芙蓉姐姐");
    	
    	//把bundle对象封装至intent对象中
    	intent.putExtras(bundle);
    	startActivity(intent);
    }
    
}


SecondActivity.java

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		Intent intent = getIntent();
		//从intent对象中把封装好的数据取出来
//		String maleName = intent.getStringExtra("malename");
//		String feMaleName = intent.getStringExtra("femalename");
		
		Bundle bundle = intent.getExtras();
		String maleName = bundle.getString("malename");
		String feMaleName = bundle.getString("femalename");
		
		Random rd = new Random();
		int yinyuan = rd.nextInt(100);
		
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setText(maleName + "和" + feMaleName + "的姻缘值为" + yinyuan);
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:112111次
    • 积分:2771
    • 等级:
    • 排名:第13747名
    • 原创:147篇
    • 转载:143篇
    • 译文:1篇
    • 评论:6条
    最新评论