第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文件,可在视图模式下修改,修改后的代码如下所示:
<?xmlversion="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. 点击前效果
点击后结果
相关文章推荐
- Android 微盘开发步骤-登录界面之JSON解析
- Android开发之路之二------第一个程序HelloWord
- 转:Android 开发环境安装步骤
- Linux下Android开发环境搭建详细步骤
- Android App widget 开发步骤
- Android开发-日期选择按钮-步骤方法
- Android开发之极光推送基本步骤
- 六步骤开发和发布自己的Android Studio类库
- Android 微盘开发步骤-登录界面之微盘登录接口接收
- 转:Android 开发环境安装步骤
- Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)
- RN 开发中android 版本升级步骤
- 【实践驱动开发2-001】wifi 在android 下的实现 - AR6000 系列移植详细步骤
- C#开发Android步骤
- JNI及Android JNI 开发基本知识和具体操作步骤
- Android开发-按钮的点击和长按-步骤方法
- android项目开发步骤
- Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是不可缺少的!
- 安卓开发 ActivieAndroid使用步骤详解,遇到的那些坑。