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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章