我们在学一种语言时,往往都是从编写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.      
- 13.    @Override     
- 14.    public void onCreate(Bundle savedInstanceState)      
- 15.    {     
- 16.        super.onCreate(savedInstanceState);     
- 17.          
- 18.        setContentView(R.layout.main);     
- 19.          
- 20.        Button bn = (Button)findViewById(R.id.ok);     
- 21.          
- 22.        bn.setOnClickListener(new OnClickListener(){     
- 23.            public void  onClick(View v)        
- 24.            {     
- 25.                  
- 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程序,然后进行相应的修改观察效果。