调用另外的layout只是能够切换页面的显示,并没有做实际的内容处理,如果想将控制权交给另外的页面操作,需要利用Intent对象,Intent对象如同英文字义,是"想要"或"意图"的意思,在主Activity中,告诉程序自己是什么,并想要前往哪里,这就是Intent对象处理的事了,下面是主Intent里面的代码:
public class ChgActivity extends Activity {
/** Called when the activity is first created. */
private Button mBtn;
private EditText mEdt;
private TextView mTv;
private Typeface mFont;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setContentView(new SampleView(this));
mTv = (TextView) findViewById(R.id.textClr);
mEdt = (EditText) findViewById(R.id.edtHeight);
mBtn = (Button) findViewById(R.id.BtnOne);
mBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
Intent itt = new Intent();
itt.setClass(ChgActivity.this, SecActivity.class); //设置需要转换的Activity
startActivity(itt); //开始新的Activity
ChgActivity.this.finish(); //结束当前的Activity
}
});
}
}
在SecActivity的Activity中,代码如下:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); //设置Activity的layout
myBtn = (Button) findViewById(R.id.BtnTwo);
myBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
itt.setClass(SecActivity.this, ChgActivity.class);
startActivity(itt);
SecActivity.this.finish();
}
});
最后还必须在AndroidManifest.xml中定义需要用到的Activity,:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChgActivity"
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="SecActivity"></activity>
</application>
</manifest>
这样就实现了两个Activity之间的转换,但是这样的转换是没什么意义的,他们之间并没有交互,如果需要参数之间的传递,还需借助另外个对象Bundle,将第一个Activity中的代码修改如下:
mBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
String str = mEdt.getText().toString(); //获取输入内容
Bundle bdl = new Bundle(); //申请Bundle变量
bdl.putString("mStr", str); //加到传入变量中
Intent itt = new Intent();
itt.putExtras(bdl); //传参
itt.setClass(ChgActivity.this, FileBrowser.class); //设置需要转换的Activity
startActivityForResult(itt, 0); //回传
}
});
并增加返回的参数处理函数如下
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode){
case RESULT_OK:
Bundle bdl = data.getExtras();
mEdt.setText(bdl.getString("mStr"));
break;
default:
break;
}
}
第二个Activity中的代码修改如下:
itt = this.getIntent();
Bundle bdl = itt.getExtras(); //获取传过来的参数
mText.setText(bdl.getString("mStr")); //显示传来的参数
myBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
itt.setClass(SecActivity.this, ChgActivity.class);
bdl.putString("mStr", str); //再往回传参
SecActivity.this.setResult(RESULT_OK, itt);
SecActivity.this.finish();
}
});
这样就可以借助Bundle对象进行两个Activity之间的参数传递了...................
public class ChgActivity extends Activity {
/** Called when the activity is first created. */
private Button mBtn;
private EditText mEdt;
private TextView mTv;
private Typeface mFont;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setContentView(new SampleView(this));
mTv = (TextView) findViewById(R.id.textClr);
mEdt = (EditText) findViewById(R.id.edtHeight);
mBtn = (Button) findViewById(R.id.BtnOne);
mBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
Intent itt = new Intent();
itt.setClass(ChgActivity.this, SecActivity.class); //设置需要转换的Activity
startActivity(itt); //开始新的Activity
ChgActivity.this.finish(); //结束当前的Activity
}
});
}
}
在SecActivity的Activity中,代码如下:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); //设置Activity的layout
myBtn = (Button) findViewById(R.id.BtnTwo);
myBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
itt.setClass(SecActivity.this, ChgActivity.class);
startActivity(itt);
SecActivity.this.finish();
}
});
最后还必须在AndroidManifest.xml中定义需要用到的Activity,:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChgActivity"
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="SecActivity"></activity>
</application>
</manifest>
这样就实现了两个Activity之间的转换,但是这样的转换是没什么意义的,他们之间并没有交互,如果需要参数之间的传递,还需借助另外个对象Bundle,将第一个Activity中的代码修改如下:
mBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
String str = mEdt.getText().toString(); //获取输入内容
Bundle bdl = new Bundle(); //申请Bundle变量
bdl.putString("mStr", str); //加到传入变量中
Intent itt = new Intent();
itt.putExtras(bdl); //传参
itt.setClass(ChgActivity.this, FileBrowser.class); //设置需要转换的Activity
startActivityForResult(itt, 0); //回传
}
});
并增加返回的参数处理函数如下
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode){
case RESULT_OK:
Bundle bdl = data.getExtras();
mEdt.setText(bdl.getString("mStr"));
break;
default:
break;
}
}
第二个Activity中的代码修改如下:
itt = this.getIntent();
Bundle bdl = itt.getExtras(); //获取传过来的参数
mText.setText(bdl.getString("mStr")); //显示传来的参数
myBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
itt.setClass(SecActivity.this, ChgActivity.class);
bdl.putString("mStr", str); //再往回传参
SecActivity.this.setResult(RESULT_OK, itt);
SecActivity.this.finish();
}
});
这样就可以借助Bundle对象进行两个Activity之间的参数传递了...................