Android项目结构和HelloWorld

[b]1、搭建开发环境[/b],见文[url=http://sducxh.iteye.com/admin/blogs/617836]配置Android开发环境[/url]
[b]2、新建工程[/b]:

1) [b]New-->Android Project[/b]

[img]http://dl.iteye.com/upload/attachment/241370/b37b1ad6-6ed5-3882-aa4b-1e1b74330ff3.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/241378/674c7e85-b9e8-3999-ad65-1e0eab5ee937.jpg[/img]

[b]展开目录结构[/b]:

[img]http://dl.iteye.com/upload/attachment/241383/cf26f88b-c203-3d06-bbda-d2f3f03bd627.jpg[/img]

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

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

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

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


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


[b]3、HelloWorld[/b]:

[b]HelloWorld.java [/b]

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);
}
}


[b]R.java[/b]

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;
}
}


[b]main.xml[/b]

<?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>



[b]strings.xml[/b]

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



[b]运行:[/b]

[img]http://dl.iteye.com/upload/attachment/241400/07a56c6c-f0cd-39b8-9d58-56bfd37ac6c2.jpg[/img]
[b]
4、布局进阶,加入button按钮监听及dialog对话框[/b]
首先将res-->layout-->main.xml文件重命名为helloworld.xml。

[b][color=red]TIP[/color][/b]:如果项目中有多个Activity,最好将layout文件的名字与 Activity对应。

[b]helloworld.xml[/b]

<?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>



[b]R.java[/b]


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;
}
}



[b]HelloWorld.java[/b]



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();
}
}


[b]strings.xml[/b]

<?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>



[b]运行:[/b]

[img]http://dl.iteye.com/upload/attachment/241403/a397ed5b-4186-36d3-a6ab-63527d372c39.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值