zoukankan      html  css  js  c++  java
  • 多个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);

  • 相关阅读:
    maven 构建 war文件&&Glassfish运行+部署war文件+访问(命令行模式)
    配置Glassfish服务器、部署Java web项目、Maven安装配置及JDK版本匹配性问题
    配置apache-maven-3.6.0时所遇到的坑(二)
    配置apache-maven-3.6.0时所遇到的坑(一)
    对Functional Language的认识
    论Injection的前世今生
    简单实现美团手机版应用源码
    WindowsPhone8可缩放图片控件的实现
    MallBuilder 多用户商城管理系统 v5.8.1.1
    Windows Phone 带文本信息的进度条
  • 原文地址:https://www.cnblogs.com/zhanglei93/p/4655654.html
Copyright © 2011-2022 走看看