android似乎已经形成了一种浪潮,做不了第一个吃螃蟹的人,只能追着去赶浪,谨在此记录下学习android的随笔,乱写乱画,切莫介意。
android是神马,以及android的安装、配置等网上大把,我就不写在这了,我用的ide是intellj,iteye上也有教程,大家可以查查。
今天就说说第一个demo,Hello,world!
先看代码:
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这个代码是由intellj自动生成的Helloworld的代码,先看看疑惑的地方:
Activity:活动,android的模块,api描述:An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)
.简单理解,即Activity代表一个用户所能看到的屏幕,通过setContentView(View)来体现你想在屏幕上展示的UI;onCreate(Bundle)
is where you initialize your activity.初始化activity,所有应用的activity都继承于android.app.Activity。
Bundle:A mapping from String values to various Parcelable types.英语不好,类似于key为String类型的map。
R:建立项目时自动生成,是该项目所有资源的索引文件,常量名和res文件夹中的文件名项目,该文件不能编辑,更新资源时,刷新该项目即可。
R.layout.main:即res/layout/main.xml,布局xml
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
xml中的LinearLayout :线性布局,以后会经常用到,后面说。
@string/hello:即res/value/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyActivity</string>
<string name="hello">Hello,world!</string>
</resources>
string.xml:资源文件,不多说,以后经常会用,包括布局,颜色,样式等;说说是如何获取这些资源的,
Context实例化Resources,再获取String
Resources r=this.getContext().getResources():
String appname=((String)r.getString(R.string.app_name));
String hello=((String)r.getString(R.string.hello));
这时还没有结束,任何应用程序,都必须在AndroidManifest.xml声明所有使用到得模块:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:label="and" android:icon="@drawable/icon">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这里主要需要关注的地方:
package:声明应用程序包;
application:包含package中application级别组件的根节点。此元素也可包含application的一些全局和默认属性,如标签、icon、主题、必要的权限等,一个manifest只能包含一个或零个此元素;
android:label:名字
android:icon:图标
activity:声明activity,每个activity必须有一个对应的标记,否则无法运行,另外,为了支持运行时查找Activity,可包含一个或多个intent-filter来描述Activity所支持的操作;
android:name:默认启动的actiivity;
intent-filter:声明指定的一组组件支持的Intent值,从而形成IntentFilter,除了能在此元素下指定不同类型的值,属性也能放到这里来描述一个操作所需的唯一标签、icon和其他信息。
action:组件支持的Intent action
category:组件支持的Intent Category。这里指定了应用程序默认启动的Activity。
uses-sdk:sdk版本
以上部分内容来自于:android api,android应用开发揭秘,android基础教程。