直接上代码,一些方法的应用在代码中有简要注释~~
FristActivity
package com.example.activitytest;
import com.example.activitytest.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不在活动中显示标题栏,注意这句代码一定要在setContentView()之前执行。
requestWindowFeature(Window.FEATURE_NO_TITLE);
//在活动中加载这个布局
setContentView(R.layout.first_layout);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
/*========显式intent========*/
//Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
//intent.putExtra("name","oneFortwo");
//startActivity(intent);
/*========隐式Intent========*/
//怎么没看到匹配指定的category呢?因为android.intent.category.DEFAULT是一种默认的category,调用时自动添加
//Intent intent = new Intent("com.example.activitytest.ACTION_START");
//也可自定义category
//intent.addCategory("android.intent.category.MY_CATEGORY");
//intent.putExtra("name","oneFortwo");
//startActivity(intent);
/*========更多隐式Intent========*/
//Intent.ACTION_VIEW,系统内置动作
//通过Uri.parse()方法,将一个网址字符串解析成一个Uri对象
//再调用setDats()方法将这个Uri对象传递
//Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.parse("http://www.baidu.com"));
//startActivity(intent);
//这又是一个内置动作,data部分的协议时Tel
//Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.parse("tel:10086"));
//startActivity(intent);
/*========传递数据和返回数据给上一个活动========*/
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
//向secondActivity传递数据
intent.putExtra("name","oneFortwo");
//第二个参数为请求码,只要传入唯一值即可
startActivityForResult(intent, 1);
}
});
Button backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("back", "销毁");
finish();
}
});
}
/**
* 获取返回结果
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK){
String returnData = data.getStringExtra("data_return");
Toast.makeText(this, returnData, Toast.LENGTH_SHORT).show();
Log.d("FirstActivity", returnData);
}
break;
default:
break;
}
}
/**
* R.menu.main:表示通过哪一个资源文件来创建菜单
* 第二个参数用于指定添加到哪一个menu中
* 返回true显示
*
* */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//获取哪一个菜单项
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(this, "you click Add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "you click Remove", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
}
SecondActivity
package com.example.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
Intent intent = getIntent();
//接收firstActivity传来的数据
Toast.makeText(this, intent.getStringExtra("name"), Toast.LENGTH_SHORT).show();
//传递返回数据
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.putExtra("data_return", "hello FirstActivity");
//此方法专门用于向上级返回数据
setResult(RESULT_OK,intent1);
finish();
}
});
}
@Override
public void onBackPressed() {
Intent intent1 = new Intent();
intent1.putExtra("data_return", "hello FirstActivity from onBackPress");
//此方法专门用于向上级返回数据
setResult(RESULT_OK,intent1);
finish();
}
}
package com.example.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
public class ThirdActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
AndroidManifest.xml实现对活动进行注册
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 注册activity -->
<activity
android:name=".FirstActivity"
android:label="this is FirstActivity">
<intent-filter>
<!-- 表示这个项目的主活动,手机上点击应用图标,首先启动的就是这个活动-->
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- 隐式intent,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action和
category等信息,然后交由系统去分析这个Intent,并帮我们找出合适的活动区启动
只有<action>,<category>中的内容同时能够匹配上Intent中指定的action,category时,这个活动才能响应Intent-->
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START"/>
<!-- 默认的category -->
<category android:name="android.intent.category.DEFAULT"/>
<!--自定义category -->
<category android:name="android.intent.category.MY_CATEGORY"/>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<!-- 默认的category -->
<category android:name="android.intent.category.DEFAULT"/>
<!--android:scheme指定了数据的协议必须时http协议,这样ThirdActivity就和浏览器啊一样了 -->
<data android:scheme="http"/>
</intent-filter>
</activity>
</application>
</manifest>
三个布局文件first_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
android:id 定义一个唯一的标识符
android:layout_width 指定了当前元素的宽度,match_parent:表示让当前元素和父元素一样宽
android:layout_height 指定了当前元素的高度,wrap_content表示当前元素的高度只要能刚好包含里面的内容就行
android:text:指定了显示的内容
-->
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonName1"
/>
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back"
/>
</LinearLayout>
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2"
/>
</LinearLayout>
third_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
android:id 定义一个唯一的标识符
android:layout_width 指定了当前元素的宽度,match_parent:表示让当前元素和父元素一样宽
android:layout_height 指定了当前元素的高度,wrap_content表示当前元素的高度只要能刚好包含里面的内容就行
android:text:指定了显示的内容
-->
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"
/>
</LinearLayout>