Android 《第一行代码》 第二章练习代码 ActivityTest
FirstActivity.java
package com.example.activitytest; 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()之前执行 ,否则会报错 java.lang.RuntimeException:
* Unable to start activity ComponentInfo:
* android.util.AndroidRuntimeException: requestFeature() must be called
* before adding content
*/
requestWindowFeature(Window.FEATURE_NO_TITLE); // 加载布局
setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// toast提示
// Toast.makeText(FirstActivity.this, "You click Button 1",
// Toast.LENGTH_SHORT).show(); // 销毁活动
// finish(); // Intent实现活动跳转,显示Intent
// Intent intent = new Intent(FirstActivity.this,
// SecondActivity.class);
// startActivity(intent); // 隐式Intent
// Intent intent = new
// Intent("com.example.activitytest.ACTION_START");
// intent.addCategory("com.example.activitytest.MY_CATEGORY");
// startActivity(intent); // 启动其他程序的活动
// Intent intent = new Intent(Intent.ACTION_DIAL);
// // intent.setData(Uri.parse("http://www.baidu.com"));
// intent.setData(Uri.parse("tel:10086"));
// startActivity(intent);
//
// 用intent传递数据
// String data = "Hello SecondActiyity";
// Intent intent = new Intent(FirstActivity.this,
// SecondActivity.class);
// intent.putExtra("extra_data", data); //键,值
// startActivity(intent); // 返回数据给上一个活动
Intent intent = new Intent(FirstActivity.this,
SecondActivity.class);
startActivityForResult(intent, 1); } });
} /* 由于我们是使用startActivityForResult()方法来启动SecondActivity 的,在SecondActivity
被销毁之后会回调上一个活动的onActivityResult()方法
第一个参数requestCode,即我们在启动活动时传入的请求码。第二个参数resultCode,即我们在返回数据时传入的处理结果。
第三个参数data,即携带着返回数据的Intent。*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String returnedData = data.getStringExtra("data_return");
Log.d("FirstActivity", returnedData);
}
break;
default:
}
} // 显示菜单
@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 clicked add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You clicked remove", Toast.LENGTH_SHORT)
.show();
break;
default:
}
return true;
}
}
SecondActivity.java
package com.example.activitytest; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button; 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 intent = getIntent();
// String data = intent.getStringExtra("extra_data");
// Log.d("SecondActivity",data); // 返回数据给上一个活动
Button button2 = (Button) findViewById(R.id.button_2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
}
});
} // 通过按下Back 键回到FirstActivity
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
} }
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" > <!--@+id/id_name:在XML中定义一个id,
@id/id_name:引用一个id
wrap_content:示当前元素的高度只要能刚好包含里面的内容就行 -->
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/> </LinearLayout>
/ActivityTest/res/menu/main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_item"
android:title="Add" />
<item
android:id="@+id/remove_item"
android:title="Remove" /> </menu>
/ActivityTest/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="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- .FirstActivity :com.example.activitytest.FirstActivity 的缩写 -->
<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> <activity android:name=".SecondActivity">
<intent-filter>
<!--<action>和<category>中的内容同时能够匹配上Intent中指定的action 和category时,
这个活动才能响应该Intent。-->
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY"/>
</intent-filter>
</activity>
<!--可以相应打开网页的intent-->
<activity android:name=".ThirdActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application> </manifest>
Android 《第一行代码》 第二章练习代码 ActivityTest的更多相关文章
- 《Android第一行代码》笔记
学习Android开发差点儿相同有两年时间了.期间也做了大大小小的一些项目.近来抽出闲暇想把Android基础强化一下,之前在网上看到了郭霖郭大神的几篇博客.从中受益不少.于是花了近一周时间看完了郭神 ...
- 《算法导论》第二章demo代码实现(Java版)
<算法导论>第二章demo代码实现(Java版) 前言 表示晚上心里有些不宁静,所以就写一篇博客,来缓缓.囧 拜读<算法导论>这样的神作,当然要做一些练习啦.除了练习题与思考题 ...
- Android艺术开发探索——第二章:IPC机制(下)
Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...
- Android开发艺术探索——第二章:IPC机制(中)
Android开发艺术探索--第二章:IPC机制(中) 好的,我们继续来了解IPC机制,在上篇我们可能就是把理论的知识写完了,然后现在基本上是可以实战了. 一.Android中的IPC方式 本节我们开 ...
- Android开发艺术探索——第二章:IPC机制(上)
Android开发艺术探索--第二章:IPC机制(上) 本章主要讲解Android的IPC机制,首先介绍Android中的多进程概念以及多进程开发模式中常见的注意事项,接着介绍Android中的序列化 ...
- Android群英传笔记——第二章:Android开发工具新接触
Android群英传笔记--第二章:Android开发工具新接触 其实这一章并没什么可讲的,前面的安装Android studio的我们可以直接跳过,如果有兴趣的,可以去看看Google主推-Andr ...
- Android 第一行代码(第二版)分享
今天从网上好不容易看到了别人转发的pdf版的 第一行代码通过下载我把它存在了百度云里面了与大家共享 http://pan.baidu.com/s/1bRztF4
- [翻译]编写高性能 .NET 代码 第二章:垃圾回收
返回目录 第二章:垃圾回收 垃圾回收是你开发工作中要了解的最重要的事情.它是造成性能问题里最显著的原因,但只要你保持持续的关注(代码审查,监控数据)就可以很快修复这些问题.我这里说的"显著的 ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少分配率, 最重要的规则,缩短对象的生命周期,减少对象层次的深度,减少对象之间的引用,避免钉住对象(Pinning)
减少分配率 这个几乎不用解释,减少了内存的使用量,自然就减少GC回收时的压力,同时降低了内存碎片与CPU的使用量.你可以用一些方法来达到这一目的,但它可能会与其它设计相冲突. 你需要在设计对象时仔细检 ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 避免使用终结器,避免大对象,避免复制缓冲区
避免使用终结器 如果没有必要,是不需要实现一个终结器(Finalizer).终结器的代码主要是让GC回收非托管资源用.它会在GC完成标记对象为可回收后,放入一个终结器队列里,在由另外一个线程执行队列里 ...
随机推荐
- (喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句
(喷血分享)利用.NET生成数据库表的创建脚本,类似SqlServer编写表的CREATE语句 在我们RDIFramework.NET代码生成器中,有这样一个应用,就是通过数据库表自动生成表的CREA ...
- Leetcode: Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- Kindle 推送教程:教你用电子邮箱推送电子书(Kindle伴侣)
Kindle 推送是什么意思?如何通过电子邮件附件推送?或许刚刚接触 Kindle 的朋友对这个概念不是很清楚,其实所谓 Kindle 推送是指亚马逊提供的一个"Kindle 个人文档服务& ...
- 分享大家一个背景为下雪的JQuery
<html><head> <meta charset="utf-8"> <meta content="IE=edge,chrom ...
- socket.io稳定性及事件测试
socket.io测试报告 1.socekt.io能坚持多久 将服务器上的socekt.io代码从早上9:30分开始运行到晚上18点,每100毫秒发送一条数据,数据大概15个字符,同时开启5个连接 结 ...
- WORD基础快捷键
选择 Alt+Shift+上下 移动整行 Ctrl+上 移动到行首 Ctrl+Shift+上下 选择到行首尾 shift+del 删除整段 ...
- spring.net
Spring.Net有两个很重要的感念就是IoC(控制反转)和DI(依赖注入). IoC.英文全称Inversion of Control.控制反转.DI.英文全称Dependency Injecti ...
- Unity随机随学
1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的步骤很多,都要将几何物体从一个坐标系中变换到另一个坐标系中去. 主要步骤有: 本地坐标->视图坐标-> ...
- fgets函数
打开文件 fopen("需要打开的路径") 然后使用fgets函数读取行 #include <stdio.h> #include <stdlib.h> #i ...
- 剑指offer——变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 问题分析 由于每次跳的阶数不确定,没有一个固定的规律,但是可以了解的是后一次跳 ...