您的位置:首页 > 移动开发 > Android开发

第1个HelloWordAndroid开发步骤

2013-09-10 18:53 225 查看

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

>

<!-- 设置按钮的id和默认的文本内容等 -->

<TextView android:id="@+id/show"

android:text=" "

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<!-- 设置按钮的id和文本内容等 -->

<Button

android:id="@+id/ok"

android:text="请点击我吧"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout>

第1个HelloWordAndroid开发步骤

1、运行Eclipse,执行“New->Other…->Android->AndroidApplication Project”命令,点击Next按钮,如下图所示:



2、在弹出的对话框中分别输入Applicatio、Project和Package的名称,并设置Android最低版本和当前版本等信息,点击Next按钮,如下图所示:



3、直接点击3次Next按钮,如下图所示







4、在弹出的对话框中,分别输入Activity和Layout名称,点击Next按钮,如下图所示:



5、项目的文件列表如下所示:



6、选中HelloWorldAndroid项目,然后打开“src->org.nsz.helloworldandroid”下的HelloWorldAndroid.java文件,自动生成的代码如下所示:

package org.nsz.helloworldandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public
class
HelloWorldAndroid extends Activity {

@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_android);
}

@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world_android, menu);
return
true
;
}
}

7、选中HelloWorldAndroid项目,然后打开“res->layout”目录下的 layout/activity_hello_world_android.xml文件,自动生成的代码如下所示:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HelloWorldAndroid">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>

</RelativeLayout>

8、此处不用做任何改动,即可运行第一个Android程序。首先选中HelloWorldAndroid项目,然后执行“Run As->Run Configuratios…”命令,在弹出的对话框中,先选中“Android Application”,再点击点击左上角的“+”按钮(即New launch configuration命令),如下图所示:



9、在弹出的对话框中先点击“Browse…”按钮,选中第1步中建立的“HelloWorldAndroid”项目;然后点击“Target”选项卡,任选一个模拟器,本文选择的是AVD 4.2,最后点击"Run"按钮运行即可。





10、第一次启动模拟器的时间较长,请耐心等候,解锁后运行结果如下图所示:

说明:按快捷键ctrl+F12可进行横竖屏的切换

a. 竖屏格式



下面再介绍使用XML布局文件和Java代码混合控制UI界面。(这是Android开发常用的模式)

11、首先设置layout/activity_hello_world_android.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"
>
<!-- 设置按钮的id和默认的文本内容等 -->

<TextView
android:id="@+id/show"

android:text=""
android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>
<!-- 设置按钮的id和文本内容等 -->
<Button
android:id="@+id/ok"
android:text="请点击我吧"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

12、修改HelloWorldAndroid.java文件(粗体字为添加的代码),修改的代码如下所示:

package org.nsz.helloworldandroid;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView; public class HelloWorldAndroid extends Activity {        @Override       protected void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              //设置使用activity_hello_world_android.xml文件定义的页面布局              setContentView(R.layout.activity_hello_world_android);              //获取UI界面中ID为R.id.ok的按钮              Buttonbn = (Button)findViewById(R.id.ok);              //为按钮绑定一个单击事件的监听器              bn.setOnClickListener(new OnClickListener(){                     public void onClick(View v)                        {                            //获取UI界面中ID为R.id.show的文本框                            final TextView show = (TextView)findViewById(R.id.show);                            //改变文本框的文本内容                            show.setText("当前时间是:" + newjava.util.Date());                     }              });       }        @Override       public boolean onCreateOptionsMenu(Menu menu) {              // Inflate the menu; this adds items to the action bar ifit is present.              getMenuInflater().inflate(R.menu.hello_world_android, menu);              return true;       }}

13、重新运行后的效果分别如下所示:

a. 点击前效果



点击后结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: