(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) 动态添加组件
(4-36)向activity传值
(4-39)从activity返回值
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();
}
});
}