Android开发之HelloWorld程序
2015-08-25 14:43
579 查看
我们在学一种语言时,往往都是从编写HelloWorld程序开始的。学习Android开发也是一样的,我们把HelloWorld程序作为Android学习之旅的开始吧。
下面直接贴代码了,这个程序比较简单,只有主Activity和main.xml文件。
主Activity文件如下:
Java代码
01.package snoopy.android.first;
02.
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.view.View;
06.import android.view.View.OnClickListener;
07.import android.widget.Button;
08.import android.widget.TextView;
09.
10.public class HelloWorldActivity extends Activity
11.{
12. //当第一次创建该Activity时回调该方法
13. @Override
14. public void onCreate(Bundle savedInstanceState)
15. {
16. super.onCreate(savedInstanceState);
17. //设置使用main.xml文件定义的页面布局
18. setContentView(R.layout.main);
19. //获取UI界面中ID为R.id.ok的按钮
20. Button bn = (Button)findViewById(R.id.ok);
21. //为按钮绑定一个单击事件的监听器
22. bn.setOnClickListener(new OnClickListener(){
23. public void onClick(View v)
24. {
25. //获取UI界面中ID为R.id.show的文本框
26. final TextView show = (TextView)findViewById(R.id.show);
27. //改变文本框的文本内容
28. show.setText("Hello Android~" + new java.util.Date());
29. }
30. });
31. }
32.}
main.xml文件内容如下:
XML/HTML代码
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
01.<!--文本框-->
01.<TextView android:id="@+id/show"
02. android:layout_width="fill_parent"
03. android:layout_height="wrap_content"
04. android:text=""
05. />
06.<!-- 设置按钮的文本为“单击我” -->
07.<Button android:text="单击我"
08. android:id="@+id/ok"
09. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"/>
11.</LinearLayout>
大家可以试着运行此Android HelloWorld程序,然后进行相应的修改观察效果。
下面直接贴代码了,这个程序比较简单,只有主Activity和main.xml文件。
主Activity文件如下:
Java代码
01.package snoopy.android.first;
02.
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.view.View;
06.import android.view.View.OnClickListener;
07.import android.widget.Button;
08.import android.widget.TextView;
09.
10.public class HelloWorldActivity extends Activity
11.{
12. //当第一次创建该Activity时回调该方法
13. @Override
14. public void onCreate(Bundle savedInstanceState)
15. {
16. super.onCreate(savedInstanceState);
17. //设置使用main.xml文件定义的页面布局
18. setContentView(R.layout.main);
19. //获取UI界面中ID为R.id.ok的按钮
20. Button bn = (Button)findViewById(R.id.ok);
21. //为按钮绑定一个单击事件的监听器
22. bn.setOnClickListener(new OnClickListener(){
23. public void onClick(View v)
24. {
25. //获取UI界面中ID为R.id.show的文本框
26. final TextView show = (TextView)findViewById(R.id.show);
27. //改变文本框的文本内容
28. show.setText("Hello Android~" + new java.util.Date());
29. }
30. });
31. }
32.}
main.xml文件内容如下:
XML/HTML代码
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:orientation="vertical"
04. android:layout_width="fill_parent"
05. android:layout_height="fill_parent"
06. >
01.<!--文本框-->
01.<TextView android:id="@+id/show"
02. android:layout_width="fill_parent"
03. android:layout_height="wrap_content"
04. android:text=""
05. />
06.<!-- 设置按钮的文本为“单击我” -->
07.<Button android:text="单击我"
08. android:id="@+id/ok"
09. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"/>
11.</LinearLayout>
大家可以试着运行此Android HelloWorld程序,然后进行相应的修改观察效果。
相关文章推荐
- Android FragmentManager BackStackRecord.run throwing NullPointerException
- Android studio无法创建新项目问题解决方案一
- Android 4.4 源码编译
- Android在不同状态下的调试方法
- 【ActionBar】Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)
- android 进程间通信---bind的前世
- 【ActionBar】Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)
- 搭建从java到android的开发环境
- android 进程间通信数据(二)------parcel的实现
- 《AndroidStudio每日一贴》7. 如何将本地变更文件移到其它的changelist?
- 关于android studio几种常见的错误解决
- Android虚拟键盘上下左右键按下和弹起的响应事件
- 关于Android NDK中调用第三方的动态库
- cordova新建项目
- Android uses-permission 权限大全
- 关于Android中EditText 常见问题小总结
- Android获取内存信息
- android studio 学习地址
- android 基本概念
- android studio跨应用启动service