zoukankan      html  css  js  c++  java
  • Android开发之HelloWorld程序

          我们在学一种语言时,往往都是从编写HelloWorld程序开始的。学习Android开发也是一样的,我们把HelloWorld程序作为Android学习之旅的开始吧。

           下面直接贴代码了,这个程序比较简单,只有主Activity和main.xml文件。

           主Activity文件如下:

    Java代码
    1. 01.package snoopy.android.first;     
    2. 02.     
    3. 03.import android.app.Activity;     
    4. 04.import android.os.Bundle;     
    5. 05.import android.view.View;       
    6. 06.import android.view.View.OnClickListener;     
    7. 07.import android.widget.Button;     
    8. 08.import android.widget.TextView;     
    9. 09.     
    10. 10.public class HelloWorldActivity extends Activity      
    11. 11.{     
    12. 12.    //当第一次创建该Activity时回调该方法      
    13. 13.    @Override     
    14. 14.    public void onCreate(Bundle savedInstanceState)      
    15. 15.    {     
    16. 16.        super.onCreate(savedInstanceState);     
    17. 17.        //设置使用main.xml文件定义的页面布局      
    18. 18.        setContentView(R.layout.main);     
    19. 19.        //获取UI界面中ID为R.id.ok的按钮      
    20. 20.        Button bn = (Button)findViewById(R.id.ok);     
    21. 21.        //为按钮绑定一个单击事件的监听器      
    22. 22.        bn.setOnClickListener(new OnClickListener(){     
    23. 23.            public void  onClick(View v)        
    24. 24.            {     
    25. 25.                //获取UI界面中ID为R.id.show的文本框      
    26. 26.                final TextView show = (TextView)findViewById(R.id.show);     
    27. 27.                //改变文本框的文本内容      
    28. 28.                show.setText("Hello Android~" + new java.util.Date());     
    29. 29.            }     
    30. 30.        });             
    31. 31.    }     
    32. 32.}    

             main.xml文件内容如下:

    XML/HTML代码
    1. 01.<?xml version="1.0" encoding="utf-8"?>     
    2. 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    3. 03.    android:orientation="vertical"     
    4. 04.    android:layout_width="fill_parent"     
    5. 05.    android:layout_height="fill_parent"     
    6. 06.    >    
    7. 01.<!--文本框-->     
    8. 01.<TextView android:id="@+id/show"      
    9. 02.    android:layout_width="fill_parent"      
    10. 03.    android:layout_height="wrap_content"      
    11. 04.    android:text=""     
    12. 05.    />     
    13. 06.<!-- 设置按钮的文本为“单击我” -->     
    14. 07.<Button android:text="单击我"      
    15. 08.    android:id="@+id/ok"      
    16. 09.    android:layout_width="wrap_content"      
    17. 10.    android:layout_height="wrap_content"/>     
    18. 11.</LinearLayout>   

            大家可以试着运行此Android HelloWorld程序,然后进行相应的修改观察效果。

  • 相关阅读:
    算法训练 表达式计算
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    New ways to verify that Multipath TCP works through your network
    TCP的拥塞控制 (Tahoe Reno NewReno SACK)
    Multipath TCP Port for Android 4.1.2
    How to enable ping response in windows 7?
    NS3
    Multipath TCP Port for Android
  • 原文地址:https://www.cnblogs.com/ablansetaimeng/p/4757406.html
Copyright © 2011-2022 走看看