Activity切換(怎麼從一個Activity到另一個Activity)
本例項程式演示的是從一個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();
}
});
}
}