讓我們看一下原代碼:
- package com.smart.activity;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class Main extends Activity {
- // ANDROID系統,組件與組件之間是通過,INTENT進行通信的。
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button) this.findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 打開新的Activity
- //第一種方法調用
- Intent intent = new Intent(Main.this,SmartActivity.class);
- // 打開應用的組件
- Main.this.startActivity(intent);
- //第二種方法調用
- // Intent intent = new Intent();
- // intent.setClass(Main.this, SmartActivity.class);
- //第三種方法調用
- // Intent intent = new Intent();
- // intent.setComponent(new ComponentName(Main.this, SmartActivity.class));
- //意思是講三種方法調查用,意是一樣的,
- //一般來講用第一種
- }
- });
- }
- }
- package com.smart.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class SmartActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //關於這行代碼,是完成界面的代碼
- super.onCreate(savedInstanceState);
- setContentView(R.layout.smart);
- Button button2 = (Button) this.findViewById(R.id.button2);
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 打開新的Activity
- //第一種方法調用
- Intent intent = new Intent(SmartActivity.this,Main.class);
- // 打開應用的組件
- SmartActivity.this.startActivity(intent);
- //第二種方法調用
- // Intent intent = new Intent();
- // intent.setClass(Main.this, SmartActivity.class);
- //第三種方法調用
- // Intent intent = new Intent();
- // intent.setComponent(new ComponentName(Main.this, SmartActivity.class));
- //意思是講三種方法調查用,意是一樣的,
- //一般來講用第一種
- }
- });
- }
- }