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

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程序,然后进行相应的修改观察效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: