Android Intent传值且实现窗体跳转
main.xml
main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <EditText
8 android:id="@+id/one"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 />
12 <EditText
13 android:id="@+id/two"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 />
17 <Button
18 android:id="@+id/btn"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="计算"
22 />
23 </LinearLayout>
result.xml
result.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/result"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 />
12 </LinearLayout>
AndroidManifest.xml
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="mars.activity"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="10" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:name=".CalcActivity"
14 android:label="@string/app_name" >
15 <intent-filter>
16 <action android:name="android.intent.action.MAIN" />
17
18 <category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 <activity
22 android:name=".ResultActivity"
23 android:label="@string/app_name" >
24 </activity>
25 </application>
26
27 </manifest>
CalcActivity.java
CalcActivity.java
1 package mars.activity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10
11 public class CalcActivity extends Activity {
12 /** Called when the activity is first created. */
13 private Button btn=null;
14 private EditText one=null;
15 private EditText two=null;
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20 btn=(Button) this.findViewById(R.id.btn);
21 one=(EditText) this.findViewById(R.id.one);
22 two=(EditText) this.findViewById(R.id.two);
23 btn.setOnClickListener(new btnOnClickListener());
24 }
25
26 class btnOnClickListener implements OnClickListener{
27
28 @Override
29 public void onClick(View v) {
30 Intent i=new Intent();
31 String onestr=one.getText().toString();
32 i.putExtra("one", onestr);
33 String twostr=two.getText().toString();
34 i.putExtra("two", twostr);
35 int one=Integer.parseInt(onestr);
36 int two=Integer.parseInt(twostr);
37 int res=one+two;
38 i.putExtra("res", res);
39 i.setClass(CalcActivity.this, ResultActivity.class);
40 startActivity(i);
41 }
42
43 }
44 }
ResultActivity.java
ResultActivity.java
1 package mars.activity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.widget.TextView;
7
8 public class ResultActivity extends Activity {
9
10 @Override
11 protected void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.result);
14 TextView result=(TextView) this.findViewById(R.id.result);
15 Intent intent=getIntent();
16 String value1=intent.getStringExtra("one");
17 String value2=intent.getStringExtra("two");
18 int res=intent.getIntExtra("res", 0);
19 result.setText(value1+"+"+value2+"="+res);
20 }
21
22 }