Intent的跳转,带返回值的跳转startActivityForResult

MainActivity

  1 package com.example.immoc;
  2 
  3 import android.app.Activity;
  4 import android.content.Intent;
  5 import android.os.Bundle;
  6 import android.util.Log;
  7 import android.view.Menu;
  8 import android.view.MenuItem;
  9 import android.view.View;
 10 import android.view.View.OnClickListener;
 11 import android.widget.Button;
 12 import android.widget.TextView;
 13 
 14 public class MainActivity extends Activity {
 15     String Tag="Tag";
 16     private Button btn1,btn2;
 17     private TextView tv;
 18     
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22         Log.i(Tag, "1.oncreate");
 23         setContentView(R.layout.activity_main);
 24         btn1=(Button) findViewById(R.id.button1);
 25         btn2=(Button) findViewById(R.id.button2);
 26         tv=(TextView) findViewById(R.id.textView2);
 27         btn1.setOnClickListener(new OnClickListener() {
 28             
 29             @Override
 30             public void onClick(View v) {
 31                 // TODO Auto-generated method stub
 32                 Intent intent=new Intent(MainActivity.this,Second.class);
 33                 MainActivity.this.startActivity(intent);
 34             }
 35         });
 36         btn2.setOnClickListener(new OnClickListener() {
 37             
 38             @Override
 39             public void onClick(View v) {
 40                 // TODO Auto-generated method stub
 41                 Intent intent2=new Intent(MainActivity.this,Second.class);
 42                 startActivityForResult(intent2, 1);
 43             }
 44         });
 45         
 46         
 47     }
 48      @Override
 49     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 50         // TODO Auto-generated method stub
 51         super.onActivityResult(requestCode, resultCode, data);
 52         if(requestCode==1&&resultCode==2){
 53             String content=data.getStringExtra("intent2");
 54             tv.setText(content);
 55         }
 56     }
 57     @Override
 58     protected void onStart() {
 59         // TODO Auto-generated method stub
 60         super.onStart();
 61         Log.i(Tag, "2.onstart");
 62     }
 63     @Override
 64     protected void onResume() {
 65         // TODO Auto-generated method stub
 66         super.onResume();
 67         Log.i(Tag, "3");
 68     }
 69     @Override
 70     protected void onStop() {
 71         // TODO Auto-generated method stub
 72         super.onStop();
 73         Log.i(Tag, "4 stop");
 74     }
 75     @Override
 76     protected void onPause() {
 77         // TODO Auto-generated method stub
 78         super.onPause();
 79         Log.i(Tag, "5 onpause");
 80     }
 81     @Override
 82     protected void onDestroy() {
 83         // TODO Auto-generated method stub
 84         super.onDestroy();
 85         Log.i(Tag, "6");
 86     }
 87     @Override
 88     protected void onRestart() {
 89         // TODO Auto-generated method stub
 90         super.onRestart();
 91         Log.i(Tag, "7");
 92     }
 93 
 94     @Override
 95     public boolean onCreateOptionsMenu(Menu menu) {
 96         // Inflate the menu; this adds items to the action bar if it is present.
 97         getMenuInflater().inflate(R.menu.main, menu);
 98         return true;
 99     }
100 
101     @Override
102     public boolean onOptionsItemSelected(MenuItem item) {
103         // Handle action bar item clicks here. The action bar will
104         // automatically handle clicks on the Home/Up button, so long
105         // as you specify a parent activity in AndroidManifest.xml.
106         int id = item.getItemId();
107         if (id == R.id.action_settings) {
108             return true;
109         }
110         return super.onOptionsItemSelected(item);
111     }
112 }

Second.java

 1 package com.example.immoc;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 
13 public class Second extends Activity {
14     
15     private EditText t1;
16     private Button btn3;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         // TODO Auto-generated method stub
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.second);
22         t1=(EditText) findViewById(R.id.ed1);
23         btn3=(Button) findViewById(R.id.button3);
24         
25         btn3.setOnClickListener(new OnClickListener() {
26             @Override
27             public void onClick(View v) {
28                 // TODO Auto-generated method stub
29                 String msg=t1.getText().toString();    
30                 
31                 Intent intent2=new Intent();
32                 intent2.putExtra("intent2",msg);
33                 setResult(2, intent2);
34                 finish();
35             }
36         });
37         
38     }
39     
40     }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.immoc.MainActivity" >
10 
11     <TextView
12         android:id="@+id/textView2"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
16 
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_alignLeft="@+id/textView1"
22         android:layout_below="@+id/textView1"
23         android:layout_marginTop="18dp"
24         android:text="start first activity" />
25 
26     <Button
27         android:id="@+id/button2"
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:layout_alignLeft="@+id/button1"
31         android:layout_below="@+id/button1"
32         android:layout_marginLeft="38dp"
33         android:text="second way to jump" />
34 
35 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:gravity="center"
 6     android:orientation="vertical" >
 7 
 8     <Button
 9         android:id="@+id/button3"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="Button" />
13     <EditText 
14         android:id="@+id/ed1"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:hint="please input anything"
18         ></EditText>
19 
20 </LinearLayout>

AndroidManifest.xml  添加activity

 1 <activity 2 android:name="com.example.immoc.Second" 3 android:theme="@android:style/Theme.DeviceDefault.Dialog"> 45 </activity> 

 

带参数的返回值:

1.startActivityForResult();

2.protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

}

3.setResult();

 

posted @ 2017-05-25 12:49  芊鸟咔咔  阅读(545)  评论(0)    收藏  举报