• Activity和Intent


    这篇文字是讲怎样创建2个Activity以及这2个Activity直接的数据传输

    1、首先创建第一个Activity,SecondLessonDemo1.java

     1 package com.example.helloworld;
     2 import android.app.Activity;
     3 import android.content.Intent;
     4 import android.net.Uri;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.view.View.OnClickListener;
     8 import android.widget.Button;
     9 
    10 public class SecondLessonDemo1 extends Activity {
    11  
    12  private Button myButton = null;
    13 
    14  @Override
    15  public void onCreate(Bundle savedInstanceState) {
    16   super.onCreate(savedInstanceState);
    17   setContentView(R.layout.activity_main);
    18   myButton = (Button) findViewById(R.id.myButton);
    19   myButton.setOnClickListener(new myButtonListener());
    20  }
    21 
    22    
    23  class myButtonListener implements OnClickListener {
    24   
    25   public void onClick(View v) {
    26    //Intent intent = new Intent();
    27    //intent.putExtra("testIntent", "123");
    28    //// 从哪里跳转,跳转到哪里去
    29    //intent.setClass(SecondLessonDemo1.this, SecondLessonDemo2.class);
    30    //SecondLessonDemo1.this.startActivity(intent);
    31    
    32    
    33    Uri uri=Uri.parse("smsto://0800000123");
    34    Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
    35    intent.putExtra("sms_body", "今天天气真好");
    36    startActivity(intent);
    37   }
    38  }
    39 }

    2、创建第二个Activity,SecondLessonDemo2.java

     1 package com.example.helloworld;
     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 SecondLessonDemo2 extends Activity {
     9 
    10  private TextView myTextView = null;
    11 
    12  @Override
    13  protected void onCreate(Bundle savedInstanceState) {
    14   // TODO Auto-generated method stub
    15   super.onCreate(savedInstanceState);
    16   setContentView(R.layout.other);
    17   Intent intent=getIntent();
    18   String value=intent.getStringExtra("testIntent");
    19   myTextView = (TextView) findViewById(R.id.myTextView);
    20   myTextView.setText(value);
    21  }
    22 
    23 }

    3、为第一个Activity创建布局文件,activity_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent" >
     5 
     6     <TextView
     7         android:id="@+id/myTextView"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:layout_centerHorizontal="true"
    11         android:layout_centerVertical="true"
    12         android:padding="@dimen/padding_medium"
    13         tools:context=".MainActivity" />
    14 
    15     <Button
    16         android:id="@+id/myButton"
    17         android:text="@+string/ok"
    18         android:layout_width="fill_parent"
    19         android:layout_height="wrap_content" />
    20 
    21 </RelativeLayout>

    4、为第二个Activity创建布局文件,other.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent" >
     5 
     6     <TextView
     7         android:id="@+id/myTextView"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"/>
    10 </RelativeLayout>

    5、注册这两个Activity

     1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2     package="com.example.helloworld"
     3     android:versionCode="1"
     4     android:versionName="1.0" >
     5 
     6     <uses-sdk
     7         android:minSdkVersion="4"
     8         android:targetSdkVersion="15" />
     9 
    10     <application
    11         android:icon="@drawable/ic_launcher"
    12         android:label="@string/app_name"
    13         android:theme="@style/AppTheme" >
    14         <activity
    15             android:name=".MainActivity"
    16             android:label="@string/title_activity_main" >
    17           
    18         </activity>
    19        
    20           <activity
    21             android:name=".SecondLessonDemo1"
    22             android:label="@string/other">
    23              <intent-filter>
    24                 <action android:name="android.intent.action.MAIN" />
    25 
    26                 <category android:name="android.intent.category.LAUNCHER" />
    27             </intent-filter>
    28         </activity>
    29           <activity
    30             android:name=".SecondLessonDemo2"
    31             android:label="dd">
    32         </activity>
    33     </application>
    34 
    35 </manifest>
    --------------------------------------------------------------------------------------------------------------------------------------------
    顺势而为
  • 相关阅读:
    浅谈SQL Server事务与锁(上篇)
    浅谈缓存技术在ASP.NET中的运用
    浅谈ASP.NET框架
    浅谈Google Chrome浏览器(操作篇)(上)
    浅谈Google Chrome浏览器(理论篇)
    linux安装字体
    【tomcat系列】详解tomcat架构(上篇)
    【tomcat系列】配置tomcat远程访问
    从实战角度超级详解中大型企业微服务化的六大核心关键技术
    Idea Intellij 终生破解版
  • 原文地址:https://www.cnblogs.com/zhuzhenyu/p/2611892.html
Copyright © 2020-2023  润新知