Android开发入门-Android里的Hello World
当我们打开eclipse,新建一个Android项目时。它会自动给我们写入一些代码:
1 package com.droidhen.game.HelloAndroid; //声明包语句
2
3 import android.app.Activity; //引入相关类
4 import android.os.Bundle; //引入相关类
5 import android.util.Log; //引入相关类
6
7 public class HelloAdroid extends Activity {
8 /** Called when the activity is first created. */
9 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.main);
13 }
14 }
有上面的代码,我们可以看到。它是通过一个函数 setContentView()来设置显示内容的。
因此,我们一样可以通过这个函数来设置显示内容。
1 TextView tv = new TextView(this);
2
3 tv.setText("Hello, Android! by Hali");
4 setContentView(tv);
5
Hello, Android! by Hali
1 //以下为所有代码
2 package com.droidhen.test.helloworld;
3
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.widget.TextView;
7
8 public class helloworld extends Activity {
9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13
14 TextView tv = new TextView(this);
15
16 tv.setText("Hello, Android! by Hali");
17 setContentView(tv);
18
19 }
20 }