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{@Overridepublic 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>


运行:


更多相关文章

  1. Android(安卓)Fragment 基本了解
  2. AndroidN新增物理按键[android7.1.2][msm8953]
  3. android深度搜索学习笔记三( 蜂鸣器驱动)
  4. android 面试(一)
  5. Android(安卓)Jni调用浅述
  6. 在android中使用文件进行数据存储
  7. Android圆形图片控件RoundedImageView
  8. 【Android开发经验】兼容不同的屏幕大小(推荐,最官方的适应屏幕大
  9. Android(安卓)apk文件拆解与重新打包

随机推荐

  1. Android在SurfaceView做动画一般方法
  2. Android(安卓)页面滑动切换
  3. Android(安卓)返回键监听
  4. Android---16---EditText中输入特定的字
  5. listview使用BaseAdapter显示图片和文字
  6. 简单有效的ItemDecoration--分割线
  7. Getting Started with Qt5 for Android
  8. Robolectric_Quick Start for Eclipse
  9. Android:New Layout Widgets: Space and
  10. Android定时发送短信完整详细示例