在安装好的 Eclipse插件的新建项目,如图所示:


大家看下面的项目结构图示 : 


src里cn.wangmeng.test下有一个 hello world,他的名字就来自于我们新建项目的时候填写的Acivity name, 这个HelloWorld就继承自Activity(Android Framework里面最重要的一个类,详细信息可以参考 -> (Activity ), 我们简单地理解为它是一个UI的容器,直接跟用户打交道最前端的类。 

还有一个R.java,这个类是 系统根据res 文件夹中的内容自动为你生成的,我们先讲一下res文件夹,res是resources的缩写,顾名思义,你 程序中所需要的文字,图片,布局文件等等 资源都是放在这个文件夹下面的,你现在看到这个文件夹下面有 
drawable - 这个是放图片的 
layout - 这个是放布局文件的 
values - 下面放字符串(strings.xml ) 

最后是AndroidManifest.xml. 你每次添加一个Acivity都需要在这个文件中描述一下,整个项目的结构都由这个文件控制。 

布局是由 XML控制的,内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView  
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <Button android:id="@+id/button"  
  13.          android:text="@string/click_me"  
  14.          android:layout_width="wrap_content"  
  15.          android:layout_height="wrap_content">  
  16. </Button>  
  17. </LinearLayout>
所有的android程序都是用XML布局的,而且布局分为很多种,布局中有两个元素,一个是文本显示框,一个是按钮,一般LinearLayout,还有相对布局,这是官方提供的布局图: 


strings.xml是存放我们常用的常量,在JAVA里面用Resources.getText() 获取,在UI里android:text="@string/click_me"获取,演示内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, helloworld!</string>
  4. <string name="app_name">helloworld</string>
  5. <string name="click_me">click_me</string>
  6. </resources>
我们在按钮添加了一个简单的响应事件,代码如下:
  1. package cn.wangmeng.test;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  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 helloworld extends Activity {
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14.        super.onCreate(savedInstanceState);
  15.        setContentView(R.layout.main);
  16.       
  17.        Button button=(Button)findViewById(R.id.button);
  18.        button.setOnClickListener(new OnClickListener()
  19.        {
  20. public void onClick(View v) {
  21. openDialog();
  22. }
  23.        }
  24.        );
  25. }
  26.  
  27. private void openDialog(){  
  28.        AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  29.        builder.setTitle("Hello");  
  30.        builder.setMessage("Hello World \n");  
  31.        builder.setNegativeButton("OK",null);  
  32.        builder.show();  
  33. }
  34. }

最后运行结果如下: