1.java
/**
*
*/
package com.itest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author Robin
*/
public class ITestP3Activity extends Activity {
Button b1;
View v1;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
//缓存View1
v1 = getLayoutInflater().inflate( R.layout.p31, null );
//动态修改v1中内容
b1 = (Button) v1.findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
Intent in = new Intent();
in.setClassName( getApplicationContext(), "com.itest.ITestP4Activity" );
startActivity( in );
}
} );
//切换到v1
setContentView( v1 );
}
}
2.java
/**
*
*/
package com.itest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author Robin
*/
public class ITestP4Activity extends Activity {
Button b1;
View v1;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
//缓存View1
v1 = getLayoutInflater().inflate( R.layout.p41, null );
//动态修改v1中内容
b1 = (Button) v1.findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
Intent in = new Intent();
in.setClassName( getApplicationContext(), "com.itest.ITestP3Activity" );
startActivity( in );
}
} );
//切换到v1
setContentView( v1 );
}
}
切换两个Activity使用了Intent类,其实常用这个类的会有调用拨打电话、发短信等外部程序
要注意的是每次的切换,都是新启动的Activity程序,可以通过下面的代码测试一下
/**
*
*/
package com.itest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* @author Robin
*/
public class ITestP3Activity extends Activity {
Button b1;
Button b2;
View v1;
TextView tv1;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
//缓存View1
v1 = getLayoutInflater().inflate( R.layout.p31, null );
//动态修改v1中内容
b1 = (Button) v1.findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
Intent in = new Intent();
in.setClassName( getApplicationContext(), "com.itest.ITestP4Activity" );
startActivity( in );
}
} );
tv1 = (TextView) v1.findViewById( R.id.textView1 );
b2 = (Button) v1.findViewById( R.id.button2 );
b2.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
tv1.setText( System.currentTimeMillis()+"" );
// TODO Auto-generated method stub
}
});
//切换到v1
setContentView( v1 );
}
}
会发现在切换之前TextView上显示的数字,在切换回来之后消失了
然后按系统的返回键,会一页一页的返回上一页(可以返回到有数字的一页),从而说明每次的切换,都是新启动的Activity程序