Activity间互相传值、动态添加组件

(4-31)
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.
An Intent is a messaging object you can use to request an action from another app component.
①建立activity类②注册到manifest.xml③.1界面绑定并呈现setContentView()
Match_parent新(建议用),fill_parent老
layout_width、layout_height必须指定,否则FC
网页--右键--查看源文件
//返回Toast对象、ms
Toast.makeText(MainActivity.this,"hello",3000).show();
点back,当前activity被destroy掉

(4-35) 动态添加组件
<LinearLayout 	android:id="@+id/linearId"
……

Button newButton=new Button(this);	//add new button
newButton.setText("new button");
LinearLayout layout=(LinearLayout)findViewById(R.id.linearId);	//需先找到其容器控件
layout.addView(newButton);
//layout.addView(newButton,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
------/
super.onCreate(savedInstanceState);
Button button=(Button)findViewById(R.id.button);
button.setText("new button");
setContentView(R.layout.activity_main);
错误:NullPointerException(因button为空),因PhoneWindow和view还未绑定,PhoneWindow为空

(4-36)向activity传值
public void onClick(View v) {
	//Intent intent=new Intent(MainActivity.this,Another.class);等价下两行
	Intent intent=new Intent();
	intent.setClass(MainActivity.this, Another.class);
	intent.putExtra("lyl","liaochunyun");
	startActivity(intent);
}
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.another);
	String string=this.getIntent().getExtras().getString("lyl");
	Toast.makeText(this,string,Toast.LENGTH_LONG).show();
}

(4-39)从activity返回值
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	Button button1=(Button)findViewById(R.id.button1);
	button1.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			Intent intent=new Intent(MainActivity.this,Another.class);
			startActivityForResult(intent, 0);	//将Intent传过去
		}
	});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {	//回调 
	super.onActivityResult(requestCode, resultCode, data);
	if (resultCode==1) {
		String string=data.getExtras().getString("lyl");
		Toast.makeText(this,string,Toast.LENGTH_LONG).show();
	}
}
------/
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.another);
	Button button2=(Button)findViewById(R.id.button2);
	
	button2.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent=getIntent();
			intent.putExtra("lyl","liaochunyun");
			setResult(1,intent);	//传回Intent
			finish();
		}
	});
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值