萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android開發之多個Activity間的交互

Android開發之多個Activity間的交互

  一、基礎知識:

  1.一個Intent對象包含了一組信息:

  1. Component name 指定啟動的Activity

  2. Action 要做什麼

  3. Data 傳送數據

  4. Category

  5. Extras 鍵值對

  6. Flags

  2.Intent基本用法:

  [java] view plaincopyprint?// 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123"); // 傳遞數據

  intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一個Activity(第二個參數)

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  // 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123"); // 傳遞數據

  intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一個Activity(第二個參數)

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  [java]

  // 接收Intent傳過來的數據

  Intent intent = getIntent();

  String value = intent.getStringExtra("testIntent"); // 接收Intent的數據

  myTextView = (TextView)findViewById(R.id.myTextView);

  //myTextView.setText(R.string.other);

  myTextView.setText(value);

  // 接收Intent傳過來的數據

  Intent intent = getIntent();

  String value = intent.getStringExtra("testIntent"); // 接收Intent的數據

  myTextView = (TextView)findViewById(R.id.myTextView);

  //myTextView.setText(R.string.other);

  myTextView.setText(value);

  3.按鈕事件的注冊:

  [java]

  private Button myButton = null;

  myButton = (Button)findViewById(R.id.myButton);

  myButton.setOnClickListener(new MyButtonListener());

  class MyButtonListener implements OnClickListener{

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  // 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123"); // 傳遞數據

  intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一個Activity(第二個參

  數)

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  /*

  Uri uri = Uri.parse("smsto:0800000123");

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  intent.putExtra("sms_body", "The SMS text");

  startActivity(intent);

  */

  }

  }

  private Button myButton = null;

  myButton = (Button)findViewById(R.id.myButton);

  myButton.setOnClickListener(new MyButtonListener());

  class MyButtonListener implements OnClickListener{

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  // 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123"); // 傳遞數據

  intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一個Activity(第二個參

  數)

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  /*

  Uri uri = Uri.parse("smsto:0800000123");

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  intent.putExtra("sms_body", "The SMS text");

  startActivity(intent);

  */

  }

  }

  二、代碼展示:

  1."Activity_02srcyanactivity_02Activity_02.java"

  [java]

  package yan.activity_02;

  import android.net.Uri;

  import android.os.Bundle;

  import android.app.Activity;

  import android.content.Intent;

  import android.view.Menu;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  public class Activity_02 extends Activity {

  private Button myButton = null;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_02);

  myButton = (Button)findViewById(R.id.myButton);

  myButton.setOnClickListener(new MyButtonListener());

  }

  class MyButtonListener implements OnClickListener{

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  // 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123");

  intent.setClass(Activity_02.this, OtherActivity.class);

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  /*

  Uri uri = Uri.parse("smsto:0800000123");

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  intent.putExtra("sms_body", "The SMS text");

  startActivity(intent);

  */

  }

  }

  }

  package yan.activity_02;

  import android.net.Uri;

  import android.os.Bundle;

  import android.app.Activity;

  import android.content.Intent;

  import android.view.Menu;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  public class Activity_02 extends Activity {

  private Button myButton = null;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_02);

  myButton = (Button)findViewById(R.id.myButton);

  myButton.setOnClickListener(new MyButtonListener());

  }

  class MyButtonListener implements OnClickListener{

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  // 生成一個Intent對象

  Intent intent = new Intent();

  intent.putExtra("testIntent", "123");

  intent.setClass(Activity_02.this, OtherActivity.class);

  //Activity_02.this.startActivity(intent);

  startActivity(intent);

  /*

  Uri uri = Uri.parse("smsto:0800000123");

  Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

  intent.putExtra("sms_body", "The SMS text");

  startActivity(intent);

  */

  }

  }

  }

  2."Activity_02srcyanactivity_02OtherActivity.java"

  [java]

  package yan.activity_02;

  import android.app.Activity;

  import android.content.Intent;

  import android.os.Bundle;

  import android.widget.TextView;

  public class OtherActivity extends Activity{

  private TextView myTextView = null;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  // TODO Auto-generated method stub

  super.onCreate(savedInstanceState);

  setContentView(R.layout.other);

  Intent intent = getIntent();

  String value = intent.getStringExtra("testIntent");

  myTextView = (TextView)findViewById(R.id.myTextView);

  //myTextView.setText(R.string.other);

  myTextView.setText(value);

  }

  }

  package yan.activity_02;

copyright © 萬盛學電腦網 all rights reserved