安卓开发-Activity-多个Activity的开发方法。

原文链接:https://blog.csdn.net/weixin_38420342/article/details/84344496

一、切换Activity的5种方式

Intent intent = new Intent();
(1)intent.setClass(this,OtherActivity.class);
(2)intent.setClassName(this,"com.xiazdong.OtherActivity");
(3)intent.setClassName("com.xiazdong","com.xiazdong.OtherActivity");//此种方式用来激活不同应用的Activity,只需要指定第一个参数:包名 为另一个应用即可;
(4)
Component comp = new Component(this,OtherActivity.class);
intent.setComponent(comp);
(5)Intent intent = new Intent(this,OtherActivity.class);
————————————————
二、发送参数与接收参数方式
1、putExtra方式:

发送
intent.putExtra("name","xiazdong");
intent.putExtra("age",20);
接收
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age");

2、Bundle方式:

发送
Bundle bundle = new Bundle();
bundle.putString("name","xiazdong");
bundle.putInt("age",20);
intent.putExtras(bundle);

接收
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");

编写一个程序,可在第一个Activity中输入两个整数,单击“计算”按钮后,在第二个Activity负责求和计算,并将结果返回

 1 <LinearLayout 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:orientation="vertical" 
 6        tools:context=".MainActivity" >
 7     
 8     <TextView
 9         android:id="@+id/tex"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="请输入数值" />
13 
14     <LinearLayout
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:orientation="horizontal" >
18 
19         <EditText
20             android:id="@+id/edt"
21             android:layout_width="0dp"
22             android:layout_height="wrap_content"
23             android:layout_weight="1" />
24 
25         <EditText
26             android:id="@+id/edt1"
27             android:layout_width="0dp"
28             android:layout_height="wrap_content"
29             android:layout_weight="1" />
30     </LinearLayout>
31 
32     <Button
33         android:id="@+id/but"
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content"
36         android:text="计算" />
37 
38     <TextView
39         android:id="@+id/tex1"
40         android:layout_width="300dp"
41         android:layout_height="50dp"
42        />
43 
44 </LinearLayout>
activity_mian.xml 程序
 1 <LinearLayout 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:orientation="vertical"
 6        tools:context=".MainActivity" >
 7  
 8 
 9      <TextView
10             android:id="@+id/e1"
11             android:layout_width="fill_parent"
12             android:layout_height="40dp"
13           />
14 
15         <TextView
16             android:id="@+id/e2"
17             android:layout_width="fill_parent"
18             android:layout_height="40dp"
19             />
20          <TextView
21             android:id="@+id/e3"
22             android:layout_width="fill_parent"
23             android:layout_height="40dp"
24             />
25   <Button
26         android:id="@+id/b1"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:text="回传" />
30 </LinearLayout>
activity_other.xml
 1 package com.example.shiyan;
 2 
 3 
 4 import android.os.Bundle;
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.TextView;
13 
14 public class MainActivity extends Activity {
15     private EditText edt;
16     private EditText edt1;
17     private TextView tex1;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         edt=(EditText)this.findViewById(R.id.edt);
23        edt=(EditText)this.findViewById(R.id.edt1);
24        tex1=(TextView)this.findViewById(R.id.tex1);
25         Button but=(Button)this.findViewById(R.id.but);
26         but.setOnClickListener(new OnClickListener()
27         {
28             public void onClick(View v) { 
29                 String number = edt.getText().toString(); 
30                 String number1 = edt.getText().toString(); 
31                 Intent intent = new Intent(); 
32                 intent.setClass(MainActivity.this,OtherActivity.class);
33                 intent.putExtra("shu1",number);
34                 intent.putExtra("shu2",number1);
35                 MainActivity.this.startActivityForResult(intent,100);
36             }
37         });
38      
39         }
40 
41     @Override
42     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
43         super.onActivityResult(requestCode, resultCode, data);
44         if(requestCode==100&&resultCode==200){
45             String a=data.getStringExtra("sum");
46             tex1.setText(a);
47             
48         }
49         
50     }
51 
52 
53     @Override
54     public boolean onCreateOptionsMenu(Menu menu) {
55         // Inflate the menu; this adds items to the action bar if it is present.
56         getMenuInflater().inflate(R.menu.main, menu);
57         return true;
58     }
59     
60 }
MainActivity
 1 package com.example.shiyan;
 2 
 3 
 4 import android.os.Bundle;
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.TextView;
13 
14 public class OtherActivity extends Activity {
15     private TextView e1;
16     private TextView e2;
17     private TextView e3;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_other);
22          e1=(TextView)this.findViewById(R.id.e1);
23          e2=(TextView)this.findViewById(R.id.e2);
24          e3=(TextView)this.findViewById(R.id.e3);
25          Intent intent=getIntent();
26          String a=intent.getStringExtra("shu1");
27          String b=intent.getStringExtra("shu2");
28          int c=Integer.parseInt(a)+Integer.parseInt(b) ;
29          String str = String.valueOf(c);
30          e1.setText(a);
31          e2.setText(b);
32          e3.setText(str);
33          Button b1=(Button)this.findViewById(R.id.b1);
34             b1.setOnClickListener(new OnClickListener()
35             {
36                 public void onClick(View v) { 
37                      Intent intent=getIntent();
38                      String a=intent.getStringExtra("shu1");
39                      String b=intent.getStringExtra("shu2");
40                      int c=Integer.parseInt(a)+Integer.parseInt(b) ;
41                      String str = String.valueOf(c);
42                     intent.putExtra("sum",str);
43                     setResult(200,intent);
44                     OtherActivity.this.finish();
45                 }
46             });    
47     }
48 
49     @Override
50     public boolean onCreateOptionsMenu(Menu menu) {
51         // Inflate the menu; this adds items to the action bar if it is present.
52         getMenuInflater().inflate(R.menu.other, menu);
53         return true;
54     }
55 
56 }
OtherActivity

 

猜你喜欢

转载自www.cnblogs.com/amazing-ld/p/12984218.html