`

Android项目结构和HelloWorld

阅读更多
1、搭建开发环境,见文配置Android开发环境
2、新建工程

1) New-->Android Project





展开目录结构:



src里com.leoms.hello下有一个HelloWorld.java,他的名字就来自于我们新建项目的时候填写的Acivity name, 这个HelloWorld就继承自Activity。

gen里com.leoms.hello的R.java,这个类是系统根据res文件夹中的内容自动生成的

res文件夹,res是resources的缩写,顾名思义,你程序中所需要的文字,图片,布局文件等等资源都是放在这个文件夹下面的,你现在看到这个文件夹下面有
drawable   - 这个是放图片的
layout       - 这个是放布局文件的
values       - 下面放字符串(strings.xml ),颜色(colors.xml ),数组(arrays.xml )

res文件夹中内容变化,R.java都会重新编译同步更新,所以这个类不需要你去手动更新了。


最后是AndroidManifest.xml. 你每次添加一个Acivity都需要在这个文件中描述一下。


3、HelloWorld

HelloWorld.java
import android.app.Activity;
import android.os.Bundle;

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


R.java
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}


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>



strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloWorld!</string>
    <string name="app_name">HelloWorld</string>
</resources>



运行:



4、布局进阶,加入button按钮监听及dialog对话框

首先将res-->layout-->main.xml文件重命名为helloworld.xml。

TIP:如果项目中有多个Activity,最好将layout文件的名字与 Activity对应。

helloworld.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">

	<Button android:id="@+id/Button01" android:text="@string/btn01_text"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>



R.java

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int Button01=0x7f050000;
    }
    public static final class layout {
        public static final int helloworld=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040002;
        public static final int btn01_text=0x7f040000;
        public static final int hello=0x7f040001;
    }
}



HelloWorld.java


import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HelloWorld extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.helloworld);

		Button button = (Button) this.findViewById(R.id.Button01);
		// 监听
		button.setOnClickListener(new OnClickListener()
		{
			public void onClick(View arg0)
			{
				openDialog();
			}

		});
	}

	/*
	 * 对话框
	 */
	private void openDialog()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Hello");
		builder.setMessage("Hello World \n");
		builder.setNegativeButton("OK", null);
		builder.show();
	}
}


strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="btn01_text">click</string>
    <string name="hello">Hello World, HelloWorld!</string>
    <string name="app_name">hello</string>
</resources>



运行:


  • 大小: 42.5 KB
  • 大小: 81.5 KB
  • 大小: 29.8 KB
  • 大小: 10.4 KB
  • 大小: 20.2 KB
分享到:
评论

相关推荐

    Android开发之旅:HelloWorld项目的目录结构

    简单地建立一个HelloWorld项目,本篇将通过HelloWorld项目来介绍Android项目的目录结构

    Android开发之旅:HelloWorld项目的目录结构.pdf

    Android开发之旅:HelloWorld项目的目录结构.pdf

    Android编程入门之HelloWorld项目目录结构分析

    我们介绍了如何搭建Android开发环境及简单地建立一个HelloWorld项目,本篇将通过HelloWorld项目来介绍Android项目的目录结构。本文的主要主题如下: 1、HelloWorld项目的目录结构 1.1、src文件夹 1.2、gen文件夹 1.3...

    react native demo工程hello world

    此demo适合初学react native,一个hello world工程,可用来学习react native项目结构,入门等使用。不喜勿喷

    [毕设季]第一讲:Android项目结构

    本节主要介绍Android开发环境的搭建、Android项目的基本结构、编写一个简单的小例子HelloWorld,带大家初步认识一下Android开发。

    Android开发相关的学习资料

    Android资源_01-Android开发环境搭建_实验01-Android开发环境搭建.pdf、实验02-Android应用开发HelloWorld.pdf; 02-Android Studio项目结构分析与调试_实验03-Android Studio项目结构分析.pdf、实验04-Android ...

    Android中利用xml文件布局修改Helloworld程序

    Android环境布置完毕,直接就是一个Helloworld程序,详情请看《利用adt-bundle轻松搭建Android开发环境与Hello world(Linux)》这使得很多人难以理清整个Android项目的基本结构。其实安卓项目,与其它Java工程的...

    Android开发之旅 完整版pdf

    • Android 开发之旅:HelloWorld项目的目录结构 • Android 开发之旅:android架构 • Android 开发之旅:应用程序基础及组件 • Android 开发之旅:应用程序基础及组件(续) • Android 开发之旅:活动与任务...

    Android开发之旅

    Android开发之旅:HelloWorld项目的目录结构 2 Android开发之旅:android架构 3 Android开发之旅:应用程序基础及组件 4 Android开发之旅:应用程序基础及组件(续) 5 Android开发之旅:活动与任务 6 Android...

    Android程序开发学习之创建第一个Android程序(Hello World程序)

    Hello World程序 当我们已经搭建好开发环境,接下来我们按照步骤创建Holle World程序 1.程序创建步骤 (1)新建项目,可以如下图点击新建一个项目, 在布局中可以根据自己的需要选择布局结构,添加组件,调节组件...

    Android开发之旅——完整版

    • Android 开发之旅:HelloWorld项目的目录结构 • Android 开发之旅:android架构 • Android 开发之旅:应用程序基础及组件 • Android 开发之旅:应用程序基础及组件(续) • Android 开发之旅:活动与任务...

    Android实验指导.doc

    Name the project HelloWorld 得到的文件结构如下: 运行: 选运行的设备,可以是模拟器,也可以是真机(如果已经连接好真实手机的话): 模拟器运行: 真实手机调试: 实验二:界面设计:控件与布局 【目的】 Android...

    Android 初识 Helloworld 详解

    HelloWorld的目录结构有: src:存放应用程序的逻辑代码,这里面的代码是人工写的gen:存放资源代码,这里面的代码是自动生成的assets:存放mp3等视频资源bin:生成的可执行的二进制文件目录libs:引用到的库,...

    中文版Android程序员入门

    项目结构 - 第六章(4) 38 在Windows CLI下创建Hello World!活动 - 第六章(5) 42 增加JAVA_HOME 第六章 (6) 43 编译并安装应用程序 第六章(7) 44 如果运行ANT时出错该怎么办? 第六章(8) 44 用adb安装你的应用...

    Android应用基础实验指导书.doc

    搭建Android平台和创建HelloWorld 实验目的 本次实验的目的是让大家熟悉搭建智能手机开发平台的过程,了解Andorid开发项目 的基本文件目录结构,并实现HelloWorld小例程。 实验要求 1. 完成Android开发平台的搭建及...

    Android程序员入门(中文版).doc

    项目结构 - 第六章(4) 38 在Windows CLI下创建Hello World!活动 - 第六章(5) 42 增加JAVA_HOME 第六章 (6) 43 编译并安装应用程序 第六章(7) 44 如果运行ANT时出错该怎么办? 第六章(8) 44 用adb安装你的应用...

    java8看不到源码-Android-N-Java8-Sample:在Android上使用Java8功能的两个示例helloworld应用程序

    指向项目结构中的 Java 8 ( ⌘ + ; ) 下载这个项目并运行它。 这个项目里面 该项目包括两个应用程序: lambda-app :让您尝试与任何设备/模拟器 API 10+ 兼容的lambda more-java8-app : 让我们来试试 Android N 才...

    android开发指南

    项目结构 - 第六章(4) 32 在Windows CLI下创建Hello World!活动 - 第六章(5) 36 增加JAVA_HOME 第六章 (6) 36 编译并安装应用程序 第六章(7) 37 如果运行ANT时出错该怎么办? 第六章(8) 37 用adb安装你的应用...

Global site tag (gtag.js) - Google Analytics