Android開發之多個Activity間的互動

來源:互聯網
上載者:User

  一、基礎知識:

  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;

  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);

  }

  }

  3."Activity_02reslayoutactivity_02.xml"

  [java]

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  android:id="@+id/myButton"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  />

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  android:id="@+id/myButton"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  />

  4."Activity_02reslayoutother.xml"

  [java]

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  android:id="@+id/myTextView"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  />

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  android:id="@+id/myTextView"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  />

  5."Activity_02resvaluesstrings.xml"

  [java]

  Activity_02

  Hello world!

  Settings

  other string

  Activity_02

  Hello world!

  Settings

  other string

  6.“Activity_02AndroidManifest.xml”

  [java] view plaincopyprint?

  package="yan.activity_02"

  android:versionCode="1"

  android:versionName="1.0" >

  android:minSdkVersion="4"

  android:targetSdkVersion="4" />

  android:allowBackup="true"

  android:icon="@drawable/ic_launcher"

  android:label="@string/app_name"

  android:theme="@style/AppTheme" >

  android:name="yan.activity_02.Activity_02"

  android:label="@string/app_name" >

  android:label="@string/other" >

  package="yan.activity_02"

  android:versionCode="1"

  android:versionName="1.0" >

  android:minSdkVersion="4"

  android:targetSdkVersion="4" />

  android:allowBackup="true"

  android:icon="@drawable/ic_launcher"

  android:label="@string/app_name"

  android:theme="@style/AppTheme" >

  android:name="yan.activity_02.Activity_02"

  android:label="@string/app_name" >

  android:label="@string/other" >

  注意這個檔案中的activity的聲明:

  android:label="@string/other" >

  三、效果展示:

  點擊上面的Button之後-->> 跳轉到另一個Activity。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.