多个Activity相互调用和Intent

MainActivity.java和OtherActivity.java的相互调用

   首先MainActivity.java是Android程序自带的,新建一个类OtherActiviy extends Activity,添加OtherActivity的onCreate方法。

       将OtherActivity在AndroidManifest.xml中注册:<activity  android:name=".OtherActivity" android:label="zhanglei"></activity>

       新建一个.xml文件用于给OtherActivity添加布局文件,命名为other.xml.

       activity_main.xml文件中有两个Button按钮:            

                   <Button
                        android:id="@+id/myButton"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@+id/textView1"
                        android:layout_below="@+id/textView1"
                        android:text="跳转" />
                  <Button
                        android:id="@+id/sendMessage"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/myButton"
                        android:text="发短信"/>

          MainActiv.java中代码如下:                 

                      private Button myButton = null; //跳转按钮
                      private Button sendMessage = null; //发送短信按钮                

                      myButton = (Button)findViewById(R.id.myButton);
                      myButton.setOnClickListener(new ButtonListener());
                      sendMessage = (Button)findViewById(R.id.sendMessage);
                      sendMessage.setOnClickListener(new SendMessageListener());

               监听器如下:

                      class SendMessageListener implements OnClickListener{

                                 @Override
                                 public void onClick(View v) {
                                          // TODO Auto-generated method stub
                                         Uri uri = Uri.parse("smsto://0800000123");
                                         Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
                                         intent.putExtra("sms_body", "The SMS text");
                                         startActivity(intent);
                                   }

                        }
                        class ButtonListener implements OnClickListener{

                                    @Override
                                    public void onClick(View v) {
                                           // TODO Auto-generated method stub
                                           Intent intent = new Intent();
                                           intent.putExtra("textIntent", "123");
                                           //第一个参数是当前Activity,第二个参数是跳转到的Activity
                                           intent.setClass(MainActivity.this, OtherActivity.class);
                                           MainActivity.this.startActivity(intent);
                                     }
                          }

            OtherAcvitivity.java代码如下:

                    textView = (TextView)findViewById(R.id.myTextView);
                    Intent intent = getIntent();
                    String value = intent.getStringExtra("textIntent");
                    textView.setText(value);

原文地址:https://www.cnblogs.com/zhanglei93/p/4655654.html