内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

Android开发之多个Activity间的交互

2016-07-21 15:29 出处:电脑高手网整理 人气: 评论(0

  一、基础知识:

  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。

相关栏目

相关文章



分享给小伙伴们:

评论

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

签名: 验证码: 点击我更换图片

评论列表

    Copyright © 2002-2016 dngsw.cn 电脑高手网 版权所有

    粤ICP备13005586号-3