Android的Hello World

Android 2.2发布了,我以前对移动开发不感兴趣的,由于工作原因,老是和这些东西打交道,于是乎,业余时间自己学点吧,程序员真辛苦。

 

怎 么搭建环境就不说了,直接从写程序开始。


import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    }
}

这个就是简单的Activity

注意,这个class是继承Activity的,Activity是用来表现一个动作的单 个个体文件,每个Activity就是一个动作,一个应用程序可以有很多的Activity,但是用户每次只和其中之一进行交互,onCreate()这 个方法是在程序每次运行的时候就会被调用,这个方法里就是你初始化所有东西和你UI的地方。每个Activity并不要求一定要有用户接口,但是一般都会 有的。

好吧,现在让我们修改一下程序。

import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
    }
}

 

 

Android的用户接口是从一个叫View的class那里继承下来的,View是一个用来画图的类,可以生成用户界面,比如图片,按钮这些东 西,在这里,TextView是一个文本类,用来显示文本的。

在这个改变中,我们在这个Activity结构里增加了一个TextView,TextView是可以用Android的Context作为参数 的,一个Context就是一个对系统处理,它提供了很多服务,比如查找资源,链接数据库等等。Activity一个Context的子类,而我们这个类 是继承的Activity,所以也是一个Context类,所以可以作为TextView的参数被传入。

TextView tv = new TextView(this);

 

下一步,我们用setText ()定义了Text的内容。

最后,为了要把它显示出来,我 们需要把这个TextView传递给setContentView()这个方法, 如果你不用这个方法的话,系统就会黑屏,什么都没有。

 

好了,运行一下吧,出现了。

 

但是如果你要是有很多UI的话,每次修改UI的内容,你都要修改源 代码,岂不是很痛苦,现在我们就说一下怎么用xml来定义。

 

上面我们完成的那个例子是 programmatic的UI layout,就是说你把你的UI直接定义在源代码里,如果你有很多UI,就会很麻烦,所以现在我们介绍另外一种方案,基于XML的UI Layout.

 

打 开main.xml

<TextView
xmlns:android ="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
/>



这个xml的结构就是用从View那里继承下来的类作为节点,这里我们只有一个节点,就是TextView.

解释一下里面各个attribute的用途,看下表



Attribute Meaning
xmlns:android This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.
android:id This attribute assigns a unique identifier to the TextView element. You can use the assigned ID to reference this View from your source code or from other XML resource declarations.
android:layout_width This attribute defines how much of the available width on the screen this View should consume. In this case, it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" means.
android:layout_height This is just like android:layout_width, except that it refers to available screen height.
android:text This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file. For more information, see Resources and Internationalization .
res/values/下面,




打开string.xml,你可以任意修改里面的内容(只要xml语法正确)

<?xml version="1.0" encoding ="utf-8"?>
<resources>
<string name ="hello">
Hello, Android! I am a string resource!
</string>
<string name ="app_name">
Hello, Android
</string>
</resources>


修改一下Activity类

import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    }
}

注意,我们去掉了TextView,运行试试看,发现显示内容和string.xml里面的一样,对吧,这样修改不是容易多了么?

 

我 会在下一篇文章里介绍一下R这个类。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值