android:跳转,Intent,有无返回值

2014-08-17

<!-- 第一个页面 -->



    <TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="126dp"

        android:text="返回的参数值" />



    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_alignRight="@+id/textView1"

        android:text="无返回值跳转" />



    <Button

        android:id="@+id/button2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/button1"

        android:layout_below="@+id/button1"

        android:text="有返回值跳转" />

 

<!-- 第二个页面 -->



    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Button" />

 

 1 //java 第一个页面

 2 

 3 public class MainActivity extends ActionBarActivity {

 4     private Button bt1;

 5     private Button bt2;

 6     private TextView tv;

 7 

 8     @Override

 9     protected void onCreate(Bundle savedInstanceState) {

10         super.onCreate(savedInstanceState);

11         setContentView(R.layout.fragment_main);

12 

13         /**

14          * 两种跳转方法

15          * 1、无返回值  使用 startActivity(intent);

16          * 2、有返回结果的跳转 使用 startActivityForResult(intent, requestCode);

17          */

18         bt1 = (Button) findViewById(R.id.button1);

19         bt2 = (Button) findViewById(R.id.button2);

20         tv = (TextView) findViewById(R.id.textView1);

21         

22         bt1.setOnClickListener(new OnClickListener() {

23             

24             @Override

25             public void onClick(View v) {

26                 // TODO 自动生成的方法存根

27                 Intent intent = new Intent(MainActivity.this, Change.class);

28                 startActivity(intent);            //第一种方法

29             }

30         });

31         

32         bt2.setOnClickListener(new OnClickListener() {

33             

34             @Override

35             public void onClick(View v) {

36                 // TODO 自动生成的方法存根

37                 Intent intent = new Intent(MainActivity.this, Change.class);

38                 /**

39                  * 第二种方法

40                  * @intent:Intent 对象

41                  * @requestCode: 请求的标识,这里写 1

42                  */

43                 startActivityForResult(intent, 1);

44             }

45         });

46     }

47     

48     

49     /**

50      * 通过startActivityForResult跳转,接收返回的数据

51      * @requestCode: 请求的标识

52      * @resultCode: 接收返回的标识

53      * @data: 接收的数据

54      */

55     @Override

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

57         // TODO 自动生成的方法存根

58         super.onActivityResult(requestCode, resultCode, data);

59         if (requestCode==1 && resultCode==2) {

60             String content = data.getStringExtra("data");

61             tv.setText(content);

62         }

63     }

64 }

 

 1 //java 第二个页面

 2 

 3 public class Change extends Activity{

 4     private Button bt;

 5     

 6     @Override

 7     protected void onCreate(Bundle savedInstanceState) {

 8         // TODO 自动生成的方法存根

 9         super.onCreate(savedInstanceState);

10         setContentView(R.layout.change);

11         

12         bt = (Button) findViewById(R.id.button1);

13         bt.setOnClickListener(new OnClickListener() {

14             

15             @Override

16             public void onClick(View v) {

17                 // TODO 自动生成的方法存根

18                 Intent data  = new Intent();

19                 data.putExtra("data", "你好");

20                 setResult(2,data);        //发送出去的标识

21                 finish();             //关闭页面

22             }

23         });

24         

25     }

26 

27 }

 

显示效果:

android:跳转,Intent,有无返回值

 

你可能感兴趣的:(android)