本实例程序演示的是从一个Activity切换到另外的一个Activity:)
程序界面
![]() |
Android开发实例——Activity切换(第一个Activity) |
![]() |
Android开发实例——Activity切换(第二个Activity) |
ActivityChange.java代码
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;
public class ActivityChange extends Activity {
private Button btn; //显示下一个Activity
private Button btnClose; //关闭
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btnNextView);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent itent=new Intent();
itent.setClass(ActivityChange.this, ActivitySecond.class);
startActivity(itent);
ActivityChange.this.finish();
}
});
btnClose=(Button)findViewById(R.id.btn_exit);
btnClose.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
ActivitySecond.java代码
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;
public class ActivitySecond extends Activity
{
private Button btn2;
private Button btnClose;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
btn2 = (Button) findViewById(R.id.btn_back);
btn2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(ActivitySecond.this, ActivityChange.class);
startActivity(intent);
//如果不关闭当前的会出现好多个页面
ActivitySecond.this.finish();
}
});
btnClose = (Button) findViewById(R.id.btn_close);
btnClose.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}