Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55273
  • 博文数量: 8
  • 博客积分: 188
  • 博客等级: 入伍新兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-30 18:31
文章分类

全部博文(8)

文章存档

2011年(8)

我的朋友

分类: 嵌入式

2011-05-25 01:47:21

伤不起啊 在word里弄好复制过来的图片又没了 困死了明天贴

Activity之间的跳转及跳转的数据传输。

有两种方法

1.startActivity(intent)

这个Intent描述了了要执行的activity,当然它也可以携带数据传到下一个activity.

以下是一个小例子:

布局文件:

R/layout/first.xml

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<Button android:id="@+id/first"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="next"

    />

LinearLayout>

 

R/layout/second.xml

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

 

<TextView android:id="@+id/text"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="8pt"

    android:text="Hello world"   //默认显示

/>

<Button android:id="@+id/second"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="refresh"

    android:layout_gravity="right"

    />

LinearLayout>

Java源文件

First.java

package com.liubin.life;

 

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;

import android.widget.TextView;

 

public class First extends Activity {

    private Button btn;

    /** Called when the activity is first created. */

    //sprivate TextView text;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.first);

        btn = (Button)findViewById(R.id.first);

        btn.setOnClickListener(new OnClickListener(){

 

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              Intent intent = new Intent();

              //存放要传输的数据

              Bundle bundle = new Bundle();

              bundle.putString("text", "I am intent posts"); //传入字串

              intent.putExtras(bundle);

              intent.setClass(First.this,Second.class);

              startActivity(intent); //跳转到下一个activitySecond

           }

        });

    }

}

 

Second.java

 

package com.liubin.life;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.app.Dialog;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

public class Second extends Activity {

 

    private Button b;

    private TextView tv;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       // TODO Auto-generated method stub

       super.onCreate(savedInstanceState);

       setContentView(R.layout.second);

       tv = (TextView)findViewById(R.id.text);

       b = (Button)findViewById(R.id.second);

       b.setOnClickListener(new OnClickListener(){

          

           @Override

           public void onClick(View arg0) {

              // TODO Auto-generated method stub

            Intent in = getIntent();

            Bundle bundle = in.getExtras();

            String text = bundle.getString("text");

            tv.setText(text);

           }

          

       });

    }

 

}

效果图:未传数据前

点击next显示以下界面

 

 

2                                   3

2为默认显示 点击refresh 显示图3内容 3 I am intent posts 为从first传入second的字符串。

2.startActivityForResult(intent, int)

         许多时候我们需要从下一个activity获得一些数据。这就要用到startActivityForResult(Intent, int), 第一个参数描述了要执行的activity,第二个int是一个requestCode 。返回的数据我们用onActivityResult(int requestCode, int resultCode, Intent)接收。子activity在调用finish()之前,要先调用setResult方法,将结果码和数据返回给父Activity。在android.app.Activity中定义两个标准结果码,RESULT_OKRESULT_CANCELED

如果子Activity启动失败或者没有显式的在setResult()中设置结果码,子Activity会默认返回RESULT_CANCELED

例子:

获得second传回的字符串显示在firsttextview

R/layout/first.xml

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView android:id="@+id/text"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="8pt"

    android:text="Hello world"

/>

<Button android:id="@+id/first"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="next"

    android:layout_gravity="right"

    />

   

LinearLayout>

R/layout/second.xml

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

 

<TextView android:id="@+id/text"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="8pt"

    android:text="Hello world"

/>

<Button android:id="@+id/second"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="back"

    android:layout_gravity="right"

    />

LinearLayout>

 

Java 代码

First.java

package com.liubin.life;

 

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;

import android.widget.TextView;

 

public class First extends Activity {

    private Button btn;

    private TextView text;

    final static int code = 1;   // 请求码

    /** Called when the activity is first created. */

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.first);

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

        btn = (Button)findViewById(R.id.first);

        btn.setOnClickListener(new OnClickListener(){

      

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              Intent intent = new Intent(First.this, Second.class);

              startActivityForResult(intent, code); //跳转到下一个activitySecond

           }

        });

    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

       // TODO Auto-generated method stub

       super.onActivityResult(requestCode, resultCode, data);

       if (requestCode == code)

       {

           if (resultCode == RESULT_OK)

           {

              String s= data.getAction();

              text.setText(s);        //设置text的值为second返回的字符串

           }

           else

           {

              System.out.println("没有返回值");

           }

       }

    }

   

}

Second.java

package com.liubin.life;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.app.Dialog;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

public class Second extends Activity {

 

         private Button b;

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   // TODO Auto-generated method stub

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.second);

                    

                   b = (Button)findViewById(R.id.second);

                   b.setOnClickListener(new OnClickListener(){

                            @Override

                            public void onClick(View arg0) {

                                     // TODO Auto-generated method stub

                                     //回传数据

                               setResult(RESULT_OK,new Intent().setAction("Hello I am second activity"));

                               finish();

                            }

                           

                   });

         }

          

        

}

 

效果图:

1                              2

1为默认显示(没有数据传回)   2 为点击图3back按钮传回的字符串显示。

3

 

请求码(requestCode):通过它我们可以对多个子Activity进行处理。

结果码(resultCode):通过它我们可以判断子Activity的处理结果,对不同的结果码进行相应的操作。

 

 

阅读(3481) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

LinuxSurfing2011-05-26 23:55:50

yifangyou: andoid里最重要的实例Intent,基本上可以调用其它很多功能.....
这个Intent确实很好啊 就像传值 也可以回传 但是既然android提供了这些东东 就应该掌握下 呵呵

yifangyou2011-05-26 21:05:49

andoid里最重要的实例Intent,基本上可以调用其它很多功能